最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

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

Python的字符串format

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

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

如果模板中{}比較多,則容易錯亂,那么在format的時候也可以指定模板數(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.

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

# 指定{}的名字w,c,b,i
template = 'Hello {w}, Hello {c}, Hello , Hello {i}.'
world = 'World'
china = 'China'
beijing = 'Beijing'
imooc = 'imooc'
# 指定名字對應(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ù)

請使用兩種format的方式打印字符串Life is short, you need Python

?不會了怎么辦

參考答案:

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

提問題

寫筆記

公開筆記
提交
||

請驗證,完成請求

由于請求次數(shù)過多,請先驗證,完成再次請求

加群二維碼

打開微信掃碼自動綁定

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

綁定后可得到

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

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

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

邀請您關(guān)注公眾號
關(guān)注后,及時獲悉本課程動態(tài)

舉報

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

手記推薦

更多

本次提問將花費2個積分

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

為什么扣積分?

本次提問將花費2個積分

繼續(xù)發(fā)表請點擊 "確定"

為什么扣積分?