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脚本来获取mp3文件tag信息的教程

下面利用一个python的实例程序,来学习python。这个程序的目的就是分析出所有MP3文件的Tag信息并输出。 import os # 导入os模块,提供文件路径,列出文件等方法 i...

Python多线程编程(三):threading.Thread类的重要函数和方法

这篇文章主要介绍threading模块中的主类Thread的一些主要方法,实例代码如下: 复制代码 代码如下: '''  Created on 2012-9-7 ...

python opencv将表格图片按照表格框线分割和识别

如下小程序为使用python+opencv将表格图片,按照表格进行分割,并识别分割后的子图片中的文字,希望对需要的小伙伴有一些些帮助。具体的实现见如下代码。 # -*- coding...

详解Python中的四种队列

详解Python中的四种队列

队列是一种只允许在一端进行插入操作,而在另一端进行删除操作的线性表。 在Python文档中搜索队列(queue)会发现,Python标准库中包含了四种队列,分别是queue.Queue...

python应用程序在windows下不出现cmd窗口的办法

python写的GTK程序,会有这样一个怪现象,本来在cmd下用 python xxx.py 启动,还好好的,但是用py2exe编译以后,再用subprocess调用命令行程序的时候,就...