Python中的format()
函數(shù)用于格式化字符串。它可以將不同類型的數(shù)據(jù)格式化為字符串中指定的格式。以下是format()
函數(shù)的各種用法及示例:
"{<參數(shù)序號(hào)>:<格式控制標(biāo)記>}".format()
格式內(nèi)容:
<.精度>-后面可以加<類型>
<類型>
整數(shù)類型:b--2進(jìn)制、c--Unicode、d--十進(jìn)制、o--八進(jìn)制、x/X--十六進(jìn)制
浮點(diǎn)數(shù)類型:
- e/E--以科學(xué)計(jì)數(shù)法的形式輸出浮點(diǎn)數(shù)
- f--以浮點(diǎn)數(shù)的形式輸出浮點(diǎn)數(shù)。默認(rèn)情況下,會(huì)輸出小數(shù)點(diǎn)后面六位數(shù)字,但是可以通過(guò)指定精度來(lái)控制輸出的位數(shù)
-
%
:--以百分?jǐn)?shù)的形式輸出浮點(diǎn)數(shù),乘以100并在末尾加上百分號(hào)。 -
s--
輸出字符串 -
r--
輸出字符串的repr()
形式(原始字符串)
1、位置參數(shù)
這是最基本的用法,可以將格式化字符串中的占位符{}
替換為相應(yīng)的位置參數(shù)。
name = "John"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
# 輸出:My name is John and I am 30 years old.
2、命名參數(shù)
可以使用命名參數(shù)來(lái)指定占位符的值,這樣可以使代碼更易讀
print("My name is {name} and I am {age} years old.".format(name="John", age=30))
# 輸出:My name is John and I am 30 years old.
3、位置參數(shù)與命名參數(shù)的混合使用
位置參數(shù)和命名參數(shù)可以混合使用,但是位置參數(shù)必須在前面。
print("My name is {0} and I am {1} years old. My hobby is {hobby}.".format("John", 30, hobby="swimming"))
# 輸出:My name is John and I am 30 years old. My hobby is swimming.
4、使用字典格式化字符串
可以使用字典的鍵值對(duì)來(lái)格式化字符串,使用{}
包含鍵名,鍵名在format()
函數(shù)中指定。
person = {'name': 'John', 'age': 30}
print("My name is {name} and I am {age} years old.".format(**person))
# 輸出:My name is John and I am 30 years old.
5、格式化數(shù)字
可以使用占位符指定數(shù)字的格式,例如{:d}
表示整數(shù),{:f}
表示浮點(diǎn)數(shù)。
num = 3.14159
print("The value of pi is approximately {:.2f}.".format(num))
# 輸出:The value of pi is approximately 3.14.
6、格式化時(shí)間
可以使用strftime()
函數(shù)指定時(shí)間的格式,然后在格式化字符串中使用占位符替換時(shí)間。
from datetime import datetime
now = datetime.now()
print("Today is {:%Y-%m-%d %H:%M:%S}.".format(now))
# 輸出:Today is 2023-04-19 18:30:00.
7、使用格式字符串語(yǔ)法
Python 3.6及以上版本支持使用f-strings(格式化字符串字面值)來(lái)格式化字符串,用法比較簡(jiǎn)單,直接在字符串前面加上字母“f”,然后使用花括號(hào){}包含變量名。
name = "John"
age = 30
print(f"My name is {name} and I am {age} years old.")
# 輸出:My name is John and I am 30 years old.
8、格式化二進(jìn)制、八進(jìn)制和十六進(jìn)制數(shù)字
可以使用占位符指定數(shù)字的進(jìn)制,例如{:b}
表示二進(jìn)制,{:o}
表示八進(jìn)制,{:x}
表示十六進(jìn)制。
num = 42
print("The binary representation of 42 is {:b}.".format(num))
# 輸出:The binary representation of 42 is 101010.
print("The octal representation of 42 is {:o}.".format(num))
# 輸出:The octal representation of 42 is 52.
print("The hexadecimal representation of 42 is {:x}.".format(num))
# 輸出:The hexadecimal representation of 42 is 2a.
9.格式化貨幣
可以使用占位符指定貨幣符號(hào)和小數(shù)點(diǎn)的位置,例如{:,.2f}
表示保留兩位小數(shù),以逗號(hào)分隔千位,使用美元符號(hào)。
price = 123456.789
print("The price is ${:,.2f}.".format(price))
# 輸出:The price is $123,456.79.
10、格式化百分比
可以使用占位符將小數(shù)轉(zhuǎn)換為百分比,例如{:.2%}
表示保留兩位小數(shù),乘以100并添加百分號(hào)。
ratio = 0.5678
print("The ratio is {:.2%}.".format(ratio))
# 輸出:The ratio is 56.78%.
11、當(dāng)需要控制字符串的寬度時(shí),可以使用占位符指定輸出的字符寬度
name = "Alice"
print("Hello, {:10}!".format(name))
# 輸出:Hello, Alice !
print("Hello, {:^10}!".format(name))
# 輸出:Hello, Alice !
print("Hello, {:<10}!".format(name))
# 輸出:Hello, Alice !
print("Hello, {:>10}!".format(name))
# 輸出:Hello, Alice!
12、如果要將字符長(zhǎng)度作為變量傳遞給格式化字符串的寬度字段,可以使用f-string(格式化字符串字面值)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-471350.html
width = 10
i = 42
formatted_string = f"{i:^{width}}"
print(formatted_string)
或者文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-471350.html
width = 10
i = 42
formatted_string = "{:^{}}".format(i, width)
print(formatted_string)
到了這里,關(guān)于format()函數(shù)的用法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!