0

I have a string

'"No, thanks, Mom," I said, "I don't know how long it will take."'

I assign it to a variable foobar

>>> foobar = """'"No, thanks, Mom," I said, "I don't know how long it will take."'"""

But when I print I get

>>> foobar
'\'"No, thanks, Mom," I said, "I don\'t know how long it will take."\''
>>>

How can I exactly print the same value of the string as I have assigned it?

1
  • what do you get when you print foobar Commented Mar 21, 2012 at 18:37

3 Answers 3

2

To print a string, use ... print:

>>> foobar = """'"No, thanks, Mom," I said, "I don't know how long it will take."'"""
>>> print(foobar)
'"No, thanks, Mom," I said, "I don't know how long it will take."'

If you just enter foobar, Python prints the representation of foobar. The representation is designed to be valid Python code and/or useful for debugging, and is not meant to be outputted to users.

Sign up to request clarification or add additional context in comments.

Comments

1
>>> print foobar

foobar, by itself, calls repr on the string, which prints the string with escapes. print foobar calls str on the string instead.

2 Comments

This (needlessly) only works in Python 2.x. I see absolutely no reason why one should teach about the print statement at the cusp of 3.x and the much easier-to-grasp print function.
@phihag: Geez, sorry, man. I've been writing Python for so long that print-as-a-statement is second nature to me; I don't even think about it.
0

It is printing the same value. It is only showing you the single quote escaped to differentiate it from the single quote used by the cmd line to display of the string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.