Python Forum
print scripts example includes comma that seems to serve no purpose
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print scripts example includes comma that seems to serve no purpose
#1
In [1]: print('Enter two integers, and I will tell you',
...: 'the relationships they satisfy.')
Enter two integers, and I will tell you the relationships they satisfy.

In [2]: print('Enter two integers, and I will tell you'
...: 'the relationships they satisfy.')
Enter two integers, and I will tell you the relationships they satisfy.

This is in ipython on anaconda and the example is out of the textbook Intro to Python. I am trying to teach myself so I did not put this in the homework section as I have no restrictions. The first thing I have learned about coding is every single thing matters. Even an errant comma.

Notice how In [1] there is a comma after you right before the line break. It is not included within the quotes so I figured it is important somehow. It does not show up in the printed statement. Now In[2] I omitted the comma in the code and it printed the same thing as the first example.

In [2] was just me experimenting because I thought it was odd the textbook did not explain the comma that was not part of the sentence. I did a google search regarding commas in python scripts but found nothing. I do not think I know enough about what I am doing to ask google the right questions yet. I found a website that talks about all the print functions but was not able to find an answer there either.

Thank you in advance.
Reply
#2
The comma separates arguments in the print command.
print(a, b)
where a = 'Enter two integers, and I will tell you' and b = 'the relationships they satisfy.'
Commas are uses as separators in function arguments, initializers for collections and dictionaries, and probably a lot of other things that don't leap to mind.
Reply
#3
Then why did it work the same without the comma? I mean couldn't I just keep pressing enter and keep going and it'll print the script no matter how long till it gets the second quote order to stop?

Also sorry mods. I will be sure to enclose any python code as per your instructions in the future. I can't find an edit button for my main thread.
Reply
#4
Without the comma the two strings are concatenated to make one string. A shorter example:
print('this is the way' 'things are')
print('this is the way', 'things are')
Output:
this is the waythings are this is the way things are
Notice the missing space in the first line. This is because Python put the two strings together making one. Automatic concatenation lets you break a really long string into multiple lines. It makes the program easier to read. In the second line there is a space. A space is the default character the print command uses as a buffer between args. The space in the printout means the print command with the comma has two args.

I think strings are the only time Python does the automatic concatenation thing. These are all errors:
print(1 2 3)
print([1, 2, 3] [4, 5, 6])
print('string' 7)
Reply
#5
They should print slightly different things.

First of all, the newline is irrelevant. You can have a linebreak in the middle of parentheses or quotes. So that doesn't matter.

In the first case, you have a print function and you've given it two elements as an argument (separated by the comma). print() can take any number of arguments, and by default it will add a space between the elements. So the two parts will print, but with a space between.

>>> print('a', 'b')
a b
In the second case, if you have multiple strings on a line with nothing but whitespace between, they are smashed together into a single string. So the second one is just print() handed a single long string (and no space is added).

>>> print('a' 'b')
ab
Reply
#6
The second one did have two words smushed together but I thought I had just made a typo so I corrected it before posting. I learned some important things from the responses on this thread thanks! I get the feeling coding is a way of thinking not just a collection of actions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,132 Aug-19-2022, 08:07 PM
Last Post: Winfried
  How to format Excel column with comma? dee 0 1,390 Jun-13-2022, 10:11 PM
Last Post: dee
  decimal comma DPaul 9 2,313 Feb-22-2022, 12:25 PM
Last Post: DeaD_EyE
  Adding a comma in the resulting value stsxbel 6 2,678 May-22-2021, 09:24 PM
Last Post: stsxbel
  How to instantly add quotation marks and comma for parameters? cheers100 4 8,155 Oct-22-2020, 12:51 PM
Last Post: cheers100
  What is the useful purpose of Python? dorlow 2 1,757 Sep-16-2020, 04:22 PM
Last Post: JaneOgden91fCL
  What is the purpose of this append command! Captain_farrell 4 2,514 Apr-29-2020, 04:34 AM
Last Post: bowlofred
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 4,131 Apr-10-2020, 02:39 PM
Last Post: buran
  Phyton code to load a comma separated csv file in to a dict and then in to a dB mrsenorchuck 2 2,686 Nov-29-2019, 10:59 AM
Last Post: mrsenorchuck
  Capture grep output to a variable which includes another variable kdefilip2 4 8,305 Nov-24-2019, 12:30 PM
Last Post: kdefilip2

Forum Jump:

User Panel Messages

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