python學(xué)習(xí)print中format的用法示例
變量插入字符串的方法
Python中的format()函數(shù)是一種將變量插入字符串的方法,能夠使字符串更易于閱讀和理解。它支持許多不同的用法,以下是具體的用法和說(shuō)明:
- 使用位置參數(shù)傳遞變量
name = 'John'
age = 25
print('My name is {}, and I am {} years old.'.format(name, age))
# 輸出:My name is John, and I am 25 years old.
- 使用索引傳遞變量
name = 'John'
age = 25
print('My name is {0}, and I am {1} years old.'.format(name, age))
# 輸出:My name is John, and I am 25 years old.
- 使用關(guān)鍵字參數(shù)傳遞變量
name = 'John'
age = 25
print('My name is {n}, and I am {a} years old.'.format(n=name, a=age))
# 輸出:My name is John, and I am 25 years old.
- 格式化數(shù)字
price = 19.99
print('The price is ${:.2f}'.format(price))
# 輸出:The price is $19.99
- 對(duì)齊文本
text = 'Hello'
print('{:>10}'.format(text)) # 右對(duì)齊輸出,總寬度為10
# 輸出: Hello
print('{:^10}'.format(text)) # 居中輸出,總寬度為10
# 輸出: Hello
print('{:<10}'.format(text)) # 左對(duì)齊輸出,總寬度為10
# 輸出:Hello
- 使用格式化字符串(Python 3.6及以上版本)
name = 'John'
age = 25
print(f'My name is {name}, and I am {age} years old.')
# 輸出:My name is John, and I am 25 years old.
- 使用字典傳遞變量
person = {'name': 'John', 'age': 25}
print('My name is {name}, and I am {age} years old.'.format(**person))
# 輸出:My name is John, and I am 25 years old.
- 使用下標(biāo)操作符獲取列表中的元素
fruits = ['apple', 'banana', 'cherry']
print('My favorite fruit is {0[1]}'.format(fruits))
# 輸出:My favorite fruit is banana
- 使用花括號(hào)轉(zhuǎn)義
print('{{Hello}}'.format()) # 輸出:{Hello}
- 使用冒號(hào)分隔格式字符串和變量名稱(chēng),對(duì)變量進(jìn)行進(jìn)一步格式化
name = 'John'
score = 95
print('Student: {0:<10} Score: {1:.2f}'.format(name, score))
# 輸出:Student: John Score: 95.00
- 根據(jù)變量類(lèi)型自動(dòng)選擇格式
x = 42
y = 3.14
print('x is {!r}, y is {!s}'.format(x, y))
# 輸出:x is 42, y is 3.14
- 使用填充字符
x = 42
print('{:0>5}'.format(x)) # 右對(duì)齊,用 0 填充,總寬度為 5
# 輸出:00042
- 根據(jù)變量類(lèi)型選擇不同的進(jìn)制輸出
x = 42
print('bin: {0:b}, oct: {0:o}, hex: {0:x}'.format(x))
# 輸出:bin: 101010, oct: 52, hex: 2a
- 自定義格式化函數(shù)
def format_salary(salary):
if salary > 10000:
return '{:.1f}K'.format(salary / 1000)
else:
return '${:,.2f}'.format(salary)
print(format_salary(5000)) # $5,000.00
print(format_salary(15000)) # 15.0K
- 使用 ** 和 * 進(jìn)行動(dòng)態(tài)參數(shù)傳遞
data = {'name': 'John', 'age': 25}
print('{name} is {age} years old.'.format(**data)) # John is 25 years old.
fruits = ['apple', 'banana', 'cherry']
print('My favorite fruits are {}, {} and {}.'.format(*fruits)) # My favorite fruits are apple, banana and cherry.
希望這些例子可以幫助您更好地理解format()函數(shù)的用法。
以上就是python學(xué)習(xí)print中format的用法示例的詳細(xì)內(nèi)容,更多關(guān)于python print format用法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python循環(huán)語(yǔ)句For?Range用法示例詳解
- Python語(yǔ)法糖for?else循環(huán)語(yǔ)句里的break使用詳解
- 詳解python架構(gòu)?PyNeuraLogic超越Transformers
- python使用for循環(huán)和海龜繪圖實(shí)現(xiàn)漂亮螺旋線
- Python中for循環(huán)可迭代對(duì)象迭代器及生成器源碼學(xué)習(xí)
- python入門(mén)學(xué)習(xí)關(guān)于for else的特殊特性講解
- Python在for循環(huán)里處理大數(shù)據(jù)的推薦方法實(shí)例
相關(guān)文章
為python爬蟲(chóng)docker鏡像添加nodejs環(huán)境實(shí)現(xiàn)方法
這篇文章主要為大家介紹了為python爬蟲(chóng)docker鏡像添加nodejs環(huán)境實(shí)現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Python圖片視頻超分模型RealBasicVSR的使用教程
這篇文章主要和大家分享一個(gè)有意思的模型:RealBasicVSR。這個(gè)模型可以實(shí)現(xiàn)圖片或視頻的超分處理,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-05-05
400多行Python代碼實(shí)現(xiàn)了一個(gè)FTP服務(wù)器
400多行Python代碼實(shí)現(xiàn)了一個(gè)FTP服務(wù)器,實(shí)現(xiàn)了比之前的xxftp更多更完善的功能2012-05-05
關(guān)于Python自動(dòng)化操作Excel
這篇文章主要介紹了關(guān)于Python自動(dòng)化操作Excel, Python 是一種功能強(qiáng)大的編程語(yǔ)言,可以用于許多任務(wù),包括處理 Excel 文件,需要的朋友可以參考下2023-04-04
在 Python 應(yīng)用中使用 MongoDB的方法
這篇文章主要介紹了在 Python 應(yīng)用中使用 MongoDB的方法,需要的朋友可以參考下2017-01-01
keras自定義損失函數(shù)并且模型加載的寫(xiě)法介紹
這篇文章主要介紹了keras自定義損失函數(shù)并且模型加載的寫(xiě)法介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
利用Python2下載單張圖片與爬取網(wǎng)頁(yè)圖片實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于利用Python2下載單張圖片與爬取網(wǎng)頁(yè)圖片的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12

