python 常用的幾種格式化方法

2022-08-15 22:36:12 字數 746 閱讀 5654

print(src + username + " 年齡是:" + str(age))

# a、需要在字串前面新增f

# b、可以在字串中間使用{},花括號的中間可以寫任何表示式(或變數)

# c、python3.6+的版本可用

info = f"

我的名字是: \t 年齡是:

"info1 = f"

我的名字是: \t 年齡是:

"print

(info)

print(info1)

# a、{} 為佔位符,format方法中的引數進行一一對應

# b、往往花括號的數量與format方法中的引數個數一致

# c、{}花括號中可以填寫format方法引數的索引值

info2 = "

我的名字是:{} \t 年齡是:{}

".format(username, age)

info3 = "

我的名字是: \t 年齡是: \t 我的分數為

".format(username, age, 99)

print

(info2)

print(info3)

info4 = "

我的名字是:%s 年齡是:%s 我的分數為:%s

" % (username, age, 88)

print("

info4:

", info4)

python格式化輸出的幾種方式

第一種 字串拼接 就不寫了 下面的是 第二 第三 第四種 name input name age int input age print type age type str age job input job salary input salary info info of s name s age...

Python基礎 常用格式化

常用格式化 百分號和format 百分號 字串拼接 s 字串 可以接收任意型別 d只能接收數字,f 2f 列印浮點數 2為取2位小數,會自動四捨五入 2f 列印百分比,print i am s alex i am alex print i am s my hobby s alex read i am...

python中幾種格式化輸出 的用法

官方文件 1.若要使用格式化字串文字,請在開始引號或三重引號之前以f或f開頭的字串。例子 year 2016 event referendum print f results of the 結果 results of the 2016 referendumyes votes 42 572 654 n...