一、舊式的字串格式化
% 操作符
參考以下示例:
>>> name = "eric"
>>> "hello, %s." % name
'hel程式設計客棧lo, eric.'
當有多個變數需要插入到字串中時:
>>> name = "eric"
>&nsueqgt;> age = 74
>>> "hello, %s. you are %s." % (name, age)
'hello, eric. you are 74.'
當需要替換的變數進一步增多時,使用 % 操作符格式化字串會導致**可讀性變得很差:
>>> first_name = "eric"
>>> last_name = "idle"
>>> age = 74
>>> profession = "comedian"
>>> affiliation = "monty python"
>>> "hello, %s %s. you are %s. you are a %s. you were a member of %s." % (first_name, last_name, age, profession, affiliation)
'hello, eric idle. you are 74. you are a comedian. you were a member of monty python.'
str.format()
str.format() 是對 % 方式的改進,它使用常見的函式呼叫的語法,並且可以通過定義物件本身的 __format__() 方法控制字串格式化的具體行為。
基本用法:
>>> name = "eric"
>>> age = 74
>>> "hello, {}. you are {}.".format(name, age)
'hello, eric. you are 74.'
str.format() 相對於 % 操作符有著更強的靈活性。比如可以通過數字索引來關聯替換到字串中的變數:
>>> name = "eric"
>>> age = 74
>>> "hello, . you are .".format(age, name)
'hello, eric. you are 74.'
為了提高**可讀性, {} 中也可以使用有具體含義的引數名:
>>> name = "eric"
>>> age = 74
>>> "hello, . you are ".format(name=name, age=age)
'hello, eric. you are 74'
針對字典結構的資料:
>>> person =
>>> "hello, . you are .".format(name=person['name'], age=person['age'])
'hello, eric. you are 74.'
或者更簡潔的方式:
>>> person =
>>> "hello, . you are .".format(**person)
'hello, eric. you are 74.'
問題在於當需要替換的變數很多時, str.format() 方式依然會導致**變得過於冗長:
>>> first_name = "eric"
>>> last_name = "idle"
>>> age = 74
>>> profession = "comedian"
>>> affiliation = "monty python"
>>> "hello, . you are . \
you are a . you were a member of ."\
.format(first_name=first_name, last_name=last_name, age=age, \
profession=profession, affiliation=affiliation)
'hello, eric idle. you are 74. you are a comedian. you were a member of monty python.'
二、f-string
基本用法
>>> name = "eric"
>&程式設計客棧gt;> age = 74
>>> f"hello, . you are ."
'hello, eric. you are 74.'
嵌入表示式
>>> f""
'74'
>>> def to_lowercase(input):
... return input.lower()
>>> name = "eric idle"
>>> f" is funny"
'eric idle is funny'
>>> f" is funny"
'eric idle is funny'
f-string 中還可以直接嵌入某個物件例項,只要其內部實現了 __str__ 或者 __repr__ 方法:
class comedian:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def __str__(self):
return f" is "
new_comedian = comedian("eric", "idle", 74)
print(f"")
# eric idle is 74
多行 f-string
>>> name = "eric"
>>> profession = www.cppcns.com"comedian"
>>> affiliation = "monty pythnsueqon"
>>> message = (
... f"hi . "
... f"you are a . "
... f"you were in ."
... )
>>> message
'hi eric. you are a comedian. you were in monty python.'
總結本文標題: 詳解python3 中的字串格式化語法
本文位址:
Python3中的字串
字串一旦建立不可更改 在字串當中每個字元都是有對應的位置的 位置一般叫做下表或者索引 小標從左到右從零開始一次遞增 在程式中根據下標線對應的資料,下表是寫在中的 建立字串 a hellow word 訪問字串種的某乙個字元 print a 7 字串的運算 加法運算時拼接操作 字串只能和整數相乘,乘幾...
python3中字串問題
在python3中,bytes string和unicodestring是兩種不同的型別。由於python3中,字串str在記憶體中是以unicode表示,乙個字元對應多個位元組。如果在網上傳輸,就需要將str轉化為以位元組為單位的bytes。例如,在做套接字試驗時,客戶端與服務端經行資料傳輸時,不...
詳解Python3中字串中的數字提取方法
逛到乙個有意思的部落格在裡面看到一篇關於valueerror invalid literal for int with base 10錯誤的解析,針對這個錯誤,博主已經給出解決辦法,使用的是re.sub 方法 totalcount 100abc totalcount re.sub d totalco...