2012-11-09 71 views
1

我是新來的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."\'。我如何正確格式化?Pyschool:主題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的行爲方式完全一樣。

+0

謝謝,我用'''而不是「」「 – NineTail