我是Python的新手,我跳到了python 3.但是我正在尝试使用pyschools.com的学习实践。我遇到了一个涉及使用字符串的问题。他们想要这个结果:"No, thanks, Mom," I said, "I don't know how long it will take."
。在Python 3中,我可以使用"""
之前和之后输入文字。但是在Python 2中,同一行出现为\'"No, thanks, Mom," I said, "I don\'t know how long it will take."\'
。如何正确格式化?
答案 0 :(得分:2)
没有问题;你的python解释器以一种可以粘贴到解释器中的方式回显给你的字符串。
如果您只想打印字符串值而不是字符串文字,请使用print()
:
>>> '''"No, thanks, Mom," I said, "I don't know how long it will take."'''
'"No, thanks, Mom," I said, "I don\'t know how long it will take."'
>>> print('''"No, thanks, Mom," I said, "I don't know how long it will take."''')
"No, thanks, Mom," I said, "I don't know how long it will take."
请注意,在这方面python版本之间没有区别; python 3的行为完全相同。