Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New line for input in Python

Tags:

python

I am very new to Python programming (15 minutes) I wanted to make a simple program that would take an input and then print it back out. This is how my code looks.

Number = raw_input("Enter a number")
print Number

How can I make it so a new line follows. I read about using \n but when I tried:

Number = raw_input("Enter a number")\n
print Number

It didn't work.

like image 584
Atul_Madhugiri Avatar asked Dec 22 '11 04:12

Atul_Madhugiri


People also ask

How do I add a new line after input?

Use the \n character to add a newline character after an input message, e.g. user_input = input('Enter a word:\n') . The \n character will add a newline character right after the input message. Copied! The \n character adds a newline right after the input message.

Is \n there in Python?

In Python, \n is a type of escape character that will create a new line when used.

How do you input a line by line in Python?

Use the input() built-in function to get a input line from the user.


2 Answers

Put it inside of the quotes:

Number = raw_input("Enter a number\n")

\n is a control character, sort of like a key on the keyboard that you cannot press.


You could also use triple quotes and make a multi-line string:

Number = raw_input("""Enter a number
""")
like image 146
Blender Avatar answered Nov 05 '22 08:11

Blender


If you want the input to be on its own line then you could also just

print "Enter a number"
Number = raw_input()
like image 41
Oliver Avatar answered Nov 05 '22 08:11

Oliver