Print() understanding \n and ,

So i have started out a course on python 3 and i am pushing through and picking up what i can but i came across something that has me confused and i am more intreuged than anything.

i am currently on the “Getting Ready for Physics Class” section of python 3 and I like to see the output of what i am doing. i was wondering if anybody can tell me why using \n in a print statement breaks if there is a “,”

Example: ( this one doesn’t work )
print("C to F Test: " + str(c0_in_fahrenheit), "\nF to C Test: " + str(f100_in_celsius))
output:
‘C to F Test: c0_in_fahrenheit’, ‘\nC to F Test: f100_in_celsius’

however this works.
print("C to F Test: " + str(c0_in_fahrenheit) + "\nF to C Test: " + str(f100_in_celsius))
Output =
C to F Test: c0_in_fahrenheit
F to C Test: f100_in_celsius

i don’t know if i need to know this but i would like to just because it interests me :slight_smile:

Hello @methodsolver93982, welcome to the forums! I don’t quite understand your question, sorry, but I’ll explain what \n does:
\n creates a new line. \ is an escape character, which means that the character after it will used for another purpose. The character n is usually, well, n, but with the \ in front of it, it becomes a character that signifies a new line. For example, the following code outputs:

print("a" + "\nb")

output:

>>a
b

The + operator, when dealing with strings, is a concatenation operator-it joins string:

print("a" + "b")

output:

>>ab

The , is used to create a tuple of values. A tuple is basically a list that cannot be mutated (changed):

a = (1, 2, 3)
a.append(4)

This throws an error message saying that you can’t append x to a tuple. The reason you don’t see the commas when it prints is to do with the way Python interprets the code.
I hope this helps!

1 Like

Thanks for your welcome! I think what i was asking was why in the first circumstance where i used the , that the new line ( \n ) didn’t work but when i replaced the , with + it worked.

I was unsure why it would matter. I am still not clear on it but you gave me some great understanding of what things are and introduced me to tuples so thanks! :slight_smile:

1 Like

Can I see your full code? I am curious why the \n didn’t work, but I currently can’t run your code

functions have been covered right? print() is also a (built-in) function. Using a comma separates the argument you pass into the function. While + does string concatenation.

Sure!

train_mass = 22680
train_acceleration = 10
train_distance = 100

bomb_mass = 1

def f_to_c(f_temp):
  return (f_temp - 32) * 5/9

def c_to_f(c_temp):
  return c_temp * (9/5) + 32

f100_in_celsius = f_to_c(100)
c0_in_fahrenheit = c_to_f(12)

print("C to F Test: " + str(c0_in_fahrenheit), "\nF to C Test: " + str(f100_in_celsius))

print("C to F Test: " + str(c0_in_fahrenheit) + "\nF to C Test: " + str(f100_in_celsius)) 

The second print statement works and creates a new line but the first one prints

 'C to F Test: 44', '\nF to C Test: 37'

Please this topic:

How to ask good questions (and get good answers)

on how to format your code please. Thank you

Sorry! still pretty new to this, i edited this for you to correctly show formatting.

I ran your code here:

https://repl.it/@stetim94/StableWebbedInterfaces#main.py

and i get:

C to F Test: 53.6 
F to C Test: 37.77777777777778
C to F Test: 53.6
F to C Test: 37.77777777777778
1 Like

Could it be what i am using? I was using visual code studio with a python3 plugin. Maybe could just be the software

If run your code in python2:

https://repl.it/@stetim94/DarkgreyPoliticalHeron#main.py

i get similar result to your problem? Can you verify what python version you use with the --version flag?

in python2, print is a statement (not a function). You will get tuples (which have been explained by @codeneutrino)

1 Like

I was indeed working on the 2.17.16 python.

I had no idea when i installed it. Thanks for your help and sorry that it was this simple. Caught a problem for the future with this though. :slight_smile: You have helped a lot so thanks for your time!

I don’t know if you know but is there a reason that print got changed from a statement to a function?
and is there a best way between a tuple and a concatenation for a case like this?

sorry for all the questions i just get interested :slight_smile: :grin:

Hello @itsnathanlevi.

Do you mean the reason that in Python 2 it is a statement, but in Python 3 it is a function? I think the main reason for this is that having print as a statement isn’t good for the evolution of the language, however, you can read more about it here.

Concatenation is when you want to join multiple strings together, and their formatting gets preserved (\n, etc). A tuple, however, is a type iterable, which means that any code within in it appear exactly as you’ve written it in the tuple.
I hope this helps!

1 Like

it does indeed help a lot! thank you very much for your time. I am very new to all of this so it really helps me understand things and get a grip of the language a lot better. Sorry for the rookie mistake with the wrong python version being the issue :slight_smile:

1 Like