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

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

WBOY
Release: 2016-06-10 15:05:14
Original
1508 people have browsed it

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

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

1 元组占位符

1

2

3

m = 'python'

astr = 'i love %s' % m

print astr

Copy after login

2 字符串的format方法

1

2

3

m = 'python'

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

print astr

Copy after login

3 字典格式化字符串

1

2

3

m = 'python'

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

print astr

Copy after login

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

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template