Python Forum

Full Version: printing strings with format
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Case 1: If I execute the following:

print('{} {}'.format('one', 'two'))
the output in the IDLE shell will be

one two

Case 2: But I execute the following in IDLE:
>>> x = '{} {}'.format('one', 'two')
>>> x
the output will be

'one two'

Why is the output without strings in the first case and with quotes in the second? I understand that there is an implicit print following the primary prompt, i.e. >>>
So why is the output different.

Case 3: Also, in the following:
After defining x, if the

>>>print(x)
The output is:

one two

Is it simply that the implicit Python print after >>> not format outputted strings without quotes?
print changes its input to a string using the str() function, and then writes to stdout the contents of that string. When you just put something in the shell and it gets echoed, the repr() function is called on it, and repr() usually tries to give a representation that could be fed back into Python.