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")括号内的字符串可以写成多行,推荐。