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迭代器过程详解

分析 我们都知道一个可迭代对象可以通过iter()可以返回一个迭代器。 如果想要一个对象称为可迭代对象,即可以使用for,那么必须实现__iter __()方法。 在一个类...

微信跳一跳python代码实现

本文实例为大家分享了python微信跳一跳的具体代码,供大家参考,具体内容如下 部分代码分享: wechat_jump.py from __future__ import print...

python保存文件方法小结

1>保存为二进制文件,pkl格式 import pickle pickle.dump(data,open('file_path','wb')) #后缀.pkl可加可不加 若文...

python中List的sort方法指南

简单记一下python中List的sort方法(或者sorted内建函数)的用法。  List的元素可以是各种东西,字符串,字典,自己定义的类等。 sorted函数用法如下:...

Flask实现跨域请求的处理方法

在Flask开发RESTful后端时,前端请求会遇到跨域的问题。下面是解决方法: 使用 flask-cors库可以很容易的解决 pip install flask-cors 两种方...