Python3 format字串格式化輸出

2021-08-21 13:02:43 字數 663 閱讀 6163

python2.6 開始,新增了一種格式化字串的函式 str.format(),它增強了字串格式化的功能。

基本語法是通過 {} 和 : 來代替以前的 % 。

format 函式可以接受不限個引數,位置可以不按順序。

# _author_:"wyq"

# date: 2018-7-18

print('{} {}'.format('hello', 'world'))

hello world

print(' '.format('hello', 'world'))

hello world

print(' '.format('hello', 'world'))

hello world hello

# 通過字典設定引數

dic =

print('姓名是: , 年齡是:'.format(**dic))

姓名是:wyq , 年齡是:26

# 通過列表索引設定引數

li = ['wyq', 26]

li1 = ['xf', 27]

print('姓名是: , 年齡是: ,另乙個人是, 年齡 '.format(li, li1))

姓名是:wyq , 年齡是:26 ,另乙個人是xf, 年齡 27

python3 format格式化輸出

使用 format 方法,可以很方便的對字串進行格式化輸出 1.使用引數位置格式 my name is age is format wang 10 my name is wang,wang age is 10 2.使用引數名 my name is age is format name wang ag...

Python字串format函式

python從2.6開始支援format,一種新的更加容易讀懂的字串格式化方法。1.替代舊的 輸出 舊的格式化輸出方法 usr bin python name tom age 18 print s is d years old name,age 使用format函式格式化輸出 usr bin pyt...

python3中的format函式

原文出處 format函式常與print 函式結合使用,具備很強的格式化輸出能力。通過變數,逗號隔開 print 今天 format 我 攔路雨 action 在寫部落格 通過關鍵字使用字典傳入,在字典前加入 grade print 比較無聊,在 format grade 字典前加上 通過位置 pr...