>>> s = 'i am %s,age %d' %('cai',18)
>>> print(s)
i am cai,age 18
>>> s = 'i am %(n1)s,age %(n2)d' %{'n1':'cai','n2':18}
>>> print(s)
i am cai,age 18
>>> s = 'i am %(n1)+10s,age %(n2)d' %{'n1':'cai','n2':18}
>>> print(s)
i am cai,age 18
>>> s = 'i am %(n1)+10s,age %(n2)10d' %{'n1':'cai','n2':18}
>>> print(s)
i am cai,age 18
>>> s = "i am %.3f abcd" %1.2
>>> print(s)
i am 1.200 abcd i1 = "i am {},age {} ,{}".format('cairui',18,'kk')
print(i1)
i am cairui,age 18 ,kk
i1 = "i am {0},age {1} ,{0}".format('cairui',18)
print(i1)
i am cairui,age 18 ,cairui
i1 = "i am {name},age {age} ,{name}".format(name='cairui',age=18)
print(i1)
i am cairui,age 18 ,cairui
i1 = "i am {:s},age {:d} ,{:f}".format('cairui',18,6.1)
print(i1)
i am cairui,age 18 ,6.100000 以上就是Python字符串格式化的方法介绍的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于Python字符串格式化的方法介绍的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did84436