Python Forum
printing strings with format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing strings with format
#1
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?
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 758 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,759 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,514 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  For loops, strings and printing words with even characters Drone4four 8 5,116 Oct-05-2018, 09:23 AM
Last Post: volcano63
  format strings syntax error VikramSuh 2 3,522 Sep-06-2018, 11:41 AM
Last Post: VikramSuh
  lists, strings, and byte strings Skaperen 2 4,217 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  printing strings on same line.. anna 3 3,048 Jan-18-2018, 10:43 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020