码迷,mamicode.com
首页 > 编程语言 > 详细

python中字符串格式化的四种方法

时间:2018-12-28 10:49:17      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:style   bsp   com   rom   div   from   字符串格式化   address   code   

1 name = "huangemiling"
2 age= 10
3 address = nanjing
4 print("My name is %s,age is %d,I come from %s"%(name,age,address))
5 print("My name is {0},age is {1},I come from {2}".format(name,age,address))
6 print("My name is {:s},age is {:d},I come from {:s}".format(name,age,address))
7 print("My name is {name},age is {age},I come from {address}".8       format(name=name,age=age,address=address))

对应的结果如下:

My name is huangemiling,age is 10,I come from nanjing
My name is huangemiling,age is 10,I come from nanjing
My name is huangemiling,age is 10,I come from nanjing
My name is huangemiling,age is 10,I come from nanjing

 

python中字符串格式化的四种方法

标签:style   bsp   com   rom   div   from   字符串格式化   address   code   

原文地址:https://www.cnblogs.com/1510152012huang/p/10188761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!