Python字符串格式化

 

 1 # 用.format格式化字符串
 2 
 3 "{} am {}".forma("I", "happy")
 4 
 5 # 可以重复参数以节省时间
 6 "{0} am {1}, {2} is {1}".format("I","happy","she")
 7  
 8 # 如果不想通过数参数,可以用关键字
 9 "{name} is hungry, and wants to eat {food}".format(name="Tom", food="fruit")
10 
11 # 使用f格式化输出
12 print(f('{"I"} am {"happy"}'))

 

 

 

posted @ 2021-06-17 23:39  术科术  阅读(33)  评论(0编辑  收藏  举报