Why can’t I make a new line with \n??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
17th Dec 2018, 7:20 AM
Brice
Brice - avatar
5 Answers
+ 7
It should be print('\n') not \n
17th Dec 2018, 7:43 AM
Lambda_Driver
Lambda_Driver - avatar
+ 6
Source Anon a = int(input("how old are you?: ")) print (a) print ("\nwhy no space??") b = int(input("\nbecause im: ")) print (b) print ("\ntogether we are: ") print (a + b) Try this instead in your test input
17th Dec 2018, 8:06 AM
BroFar
BroFar - avatar
+ 5
Even though you probably got it by now, I just want to make two tiny annotations: 1) there are several special symbols like line breaks (\n), tabs (\t), carriage returns (\r), null characters (\0) etc. that are part of the ASCII code. That's why in order to use them, you need to print them just like regular letters of the alphabet. Python doesn't know what to do with the statement "\n" because a line break is not part of python's syntax, but of the ASCII "alphabet". 2) python's print() method is somewhat special. Whenever you print a string like this: print('Hello World') ...it will automatically end with a line break. That's because there is a "hidden" end parameter that is automatically set to '\n' unless you change it to something else. So the statement actually looks like this: print('Hello World', end='\n') This means: "print 'Hello World' to the console and end with a line break". That's why if you use this statement: print('\n') what you actually get is this: print('\n', end='\n') So you'll end up with two (!) line breaks. To avoid that, you can either do this: print() # print an empty string, end with a line break because of the hidden "end" parameter which is '\n' or this: print('\n', end='') # print a line break, set the "end" parameter to an empty string, i.e. print only one line break and that's it.
17th Dec 2018, 8:56 AM
Anna
Anna - avatar
+ 3
You can't just write it on its own, you need to print it as a string. print("\n") The print statement however has an implicit \n at the end anyway, so by default you will print into a new line.
17th Dec 2018, 7:42 AM
Matthias
Matthias - avatar
+ 1
This is an enrichment in addition to Anna's lesson: https://code.sololearn.com/cTiay1gR16w3/?ref=app
17th Dec 2018, 10:44 AM
Gordon
Gordon - avatar