Python Forum
print scripts example includes comma that seems to serve no purpose - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: print scripts example includes comma that seems to serve no purpose (/thread-29434.html)



print scripts example includes comma that seems to serve no purpose - flour_power_33 - Sep-02-2020

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.


RE: print scripts example includes comma that seems to serve no purpose - deanhystad - Sep-02-2020

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.


RE: print scripts example includes comma that seems to serve no purpose - flour_power_33 - Sep-02-2020

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.


RE: print scripts example includes comma that seems to serve no purpose - deanhystad - Sep-02-2020

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)



RE: print scripts example includes comma that seems to serve no purpose - bowlofred - Sep-02-2020

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



RE: print scripts example includes comma that seems to serve no purpose - flour_power_33 - Sep-02-2020

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.