第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

章節(jié)
問答
課簽
筆記
評(píng)論
占位
占位

Python的字符串format

字符串是Python程序重要的數(shù)據(jù)類型,到目前為止,我們輸出的字符串的內(nèi)容都是固定的,但有時(shí)候通過字符串輸出的內(nèi)容不是固定的,這個(gè)時(shí)候需要使用format來處理字符串,輸出不固定的內(nèi)容。
字符串format由兩個(gè)部分組成,字符串模板和模板數(shù)據(jù)內(nèi)容組成,通過大括號(hào){},就可以把模板數(shù)據(jù)內(nèi)容嵌到字符串模板對(duì)應(yīng)的位置。

# 字符串模板
template = 'Hello {}'
# 模板數(shù)據(jù)內(nèi)容
world = 'World'
result = template.format(world)
print(result) # ==> Hello World

如果模板中{}比較多,則容易錯(cuò)亂,那么在format的時(shí)候也可以指定模板數(shù)據(jù)內(nèi)容的順序。

# 指定順序
template = 'Hello {0}, Hello {1}, Hello {2}, Hello {3}.'
result = template.format('World', 'China', 'Beijing', 'imooc')
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.
# 調(diào)整順序
template = 'Hello {3}, Hello {2}, Hello {1}, Hello {0}.'
result = template.format('World', 'China', 'Beijing', 'imooc')
print(result) # ==> Hello imooc, Hello Beijing, Hello China, Hello World.

除了使用順序,還可以指定對(duì)應(yīng)的名字,使得在format過程更加清晰。

# 指定{}的名字w,c,b,i
template = 'Hello {w}, Hello {c}, Hello , Hello {i}.'
world = 'World'
china = 'China'
beijing = 'Beijing'
imooc = 'imooc'
# 指定名字對(duì)應(yīng)的模板數(shù)據(jù)內(nèi)容
result = template.format(w = world, c = china, b = beijing, i = imooc)
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.

任務(wù)

請(qǐng)使用兩種format的方式打印字符串Life is short, you need Python。

?不會(huì)了怎么辦

參考答案:

print('Life is short, you need {}'.format('Python'))
print('Life is short, you need {launguage}'.format( launguage = 'Python'))
||

提問題

寫筆記

公開筆記
提交
||

請(qǐng)驗(yàn)證,完成請(qǐng)求

由于請(qǐng)求次數(shù)過多,請(qǐng)先驗(yàn)證,完成再次請(qǐng)求

加群二維碼

打開微信掃碼自動(dòng)綁定

您還未綁定服務(wù)號(hào)

綁定后可得到

  • · 粉絲專屬優(yōu)惠福利
  • · 大咖直播交流干貨
  • · 課程更新,問題答復(fù)提醒
  • · 賬號(hào)支付安全提醒

收藏課程后,能更快找到我哦~

使用 Ctrl+D 可將課程添加到書簽

邀請(qǐng)您關(guān)注公眾號(hào)
關(guān)注后,及時(shí)獲悉本課程動(dòng)態(tài)

舉報(bào)

0/150
提交
取消
全部 精華 我要發(fā)布
全部 我要發(fā)布
最熱 最新
只看我的

手記推薦

更多

本次提問將花費(fèi)2個(gè)積分

你的積分不足,無法發(fā)表

為什么扣積分?

本次提問將花費(fèi)2個(gè)積分

繼續(xù)發(fā)表請(qǐng)點(diǎn)擊 "確定"

為什么扣積分?