Home  >  Article  >  Backend Development  >  Python字符串格式化输出方法分析

Python字符串格式化输出方法分析

WBOY
WBOYOriginal
2016-06-10 15:05:141439browse

本文实例分析了Python字符串格式化输出方法。分享给大家供大家参考,具体如下:

我们格式化构建字符串可以有3种方法:

1 元组占位符

m = 'python'
astr = 'i love %s' % m
print astr

2 字符串的format方法

m = 'python'
astr = "i love {python}".format(python=m)
print astr

3 字典格式化字符串

m = 'python'
astr = "i love %(python)s " % {'python':m}
print astr

大家可以根据自己的实际情况来选择合适的方法,推荐用字符串的format方法或者字典的占位格式化,因为它不会受参数的位置影响,只需要参数名称相同就行。

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn