首页 > 编程 > Python > 正文

python输入多行字符串的方法总结

2019-11-25 12:34:12
字体:
来源:转载
供稿:网友

Python中输入多行字符串:

方法一:使用三引号

>>> str1 = '''Le vent se lève, il faut tenter de vivre. 起风了,唯有努力生存。(纵有疾风起,人生不言弃。)''' >>> str1'Le vent se lève, il faut tenter de vivre. /n起风了,唯有努力生存。/n(纵有疾风起,人生不言弃。)' >>> print(str1)Le vent se lève, il faut tenter de vivre. 起风了,唯有努力生存。(纵有疾风起,人生不言弃。)

方法二:使用反斜杠

>>> str2 = 'Le vent se lève, il faut tenter de vivre. /起风了,唯有努力生存。/(纵有疾风起,人生不言弃。)' >>> str2'Le vent se lève, il faut tenter de vivre. 起风了,唯有努力生存。(纵有疾风起,人生不言弃。)'

方法三:使用小括号

>>> str3 = ('Le vent se lève, il faut tenter de vivre.''起风了,唯有努力生存。''(纵有疾风起,人生不言弃。)') >>> str3'Le vent se lève, il faut tenter de vivre.起风了,唯有努力生存。(纵有疾风起,人生不言弃。)'

扩展:

问题

有一个字符串很长,如何写成多行?

解决

方法一

使用续行符:

sql = "select * "/" from a "/" where b = 1"

但是高版本python可能会不支持此方式,且每次都要在行最后加上续行符,不够简洁。

方法二

使用括号:

sql = ("select *"" from a "" where b = 1")

括号内的字符串可以写成多行,推荐。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表