Python 字符串换行的多种方式

yipeiwu_com5年前Python基础

第一种:

x0 = '<?xml version="1.0"?>' \
   '<ol>' \
   ' <li><a href="/python" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Python</a></li>' \
   ' <li><a href="/ruby" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Ruby</a></li>' \
   '</ol>'

第二种:

x1 = '<?xml version="1.0"?> \
<ol> \
  <li><a href="/python" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Python</a></li> \
  <li><a href="/ruby" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Ruby</a></li> \
</ol>'

第三种:

x2 = ('<?xml version="1.0"?>'
   '<ol>'
   ' <li><a href="/python" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Python</a></li>'
   ' <li><a href="/ruby" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Ruby</a></li>'
   '</ol>')

第四种:

x3 = '''<?xml version="1.0"?>
<ol>
  <li><a href="/python" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Python</a></li>
  <li><a href="/ruby" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Ruby</a></li>
</ol>'''

下面看下python代码过长的换行

python代码换行就是每行后面加个 \

举个栗子:

time = "2017"
 print "one" + "," \
 + "two" \
 + ",three" + \
 "," + time

打印出来就是:

one,two,three,2017

再举一个栗子:

print "this line is toooooooooooo \
 long"

打印出来:

this line is toooooooooooo long

总结

以上所述是小编给大家介绍的Python 字符串换行的多种方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

使用Python编写一个模仿CPU工作的程序

今天早上早些时候,在我的Planet Python源中,我读到了一篇有趣的文章"开发CARDIAC:纸板计算机(Developing upwards: CARDIAC: The Card...

python实现pdf转换成word/txt纯文本文件

本文实例为大家分享了python实现pdf转word/txt,供大家参考,具体内容如下 依赖包:pdfminer3k 可以通过pip安装;也可以到官网下载,解压,进入文件夹,输入命令s...

python实现动态创建类的方法分析

本文实例讲述了python实现动态创建类的方法。分享给大家供大家参考,具体如下: python作为动态语言,如何在运行时动态创建类呢(python Creating classes dy...

python 实现对数据集的归一化的方法(0-1之间)

多数情况下,需要对数据集进行归一化处理,再对数据进行分析 #首先,引入两个库 ,numpy,sklearn from sklearn.preprocessing import Mi...

Python使用matplotlib实现的图像读取、切割裁剪功能示例

Python使用matplotlib实现的图像读取、切割裁剪功能示例

本文实例讲述了Python使用matplotlib实现的图像读取、切割裁剪功能。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- import sys...