我是 Python 新手,我直接进入了 Python 3。但是我正在尝试使用 pyschools.com 的学习实践。我遇到了一个处理使用字符串的问题。他们想要这个结果:"No, thanks, Mom," I said, "I don't know how long it will take."
. 在 Python 3 中,我可以使用"""
before 和 after 输入这个文字。但是在 Python 2 中,同一行显示为\'"No, thanks, Mom," I said, "I don\'t know how long it will take."\'
. 如何正确格式化?
问问题
856 次
1 回答
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 的行为方式完全相同。
于 2012-11-09T17:52:33.680 回答