Python course · Module 2: Values · Lesson 10 of 63
Quick answer: The Python input() function shows a prompt, waits for the user to type a line and press Enter, and returns what they typed as a string. Even if the user types 21, you get the text "21", so convert it with int(input("Age: ")) or float(...) before doing maths. For output, print() shows values separated by a space and ends with a new line; change that with sep= and end=.
In this lesson you will learn:
- how to control
print()output withsepandend - how to read text from the keyboard with
input()and a prompt - why
input()always returns a string, and how to convert it - what happens when the user types something you cannot convert
- how to build a small interactive program: a bill splitter
Before you start: finish Lesson 9, Python Operators, and Lesson 8, where you learned int() and float(). Run the scripts in this lesson from a terminal (Lesson 3), because input() needs a place where you can type. All examples were tested on Python 3.14.6 and behave the same on Python 3.15.2, the newest release on python.org.
Table of Contents
print() with sep and end
You have used print() since Lesson 1. When you give it several values separated by commas, it puts a space between them and a new line at the end. print() with nothing inside prints an empty line.
name = "Ayesha"
age = 21
print("Name:", name, "Age:", age)
print()
print("Done")
Name: Ayesha Age: 21
Done
The built-in functions page shows the full form: print(*objects, sep=' ', end='\n', file=None, flush=False). So sep (the separator) is a space by default, and end is a new line \n. You can change both:
print("2026", "11", "04", sep="-")
print("Tea", "Sandwich", "Juice", sep=", ")
print("Line 1", "Line 2", sep="\n")
print("Loading", end="")
print("...", end=" ")
print("done!")
2026-11-04
Tea, Sandwich, Juice
Line 1
Line 2
Loading... done!
sep="-"joins the three values with a dash, which is handy for dates.sep="\n"puts each value on its own line.end=""stopsprint()from starting a new line, so the nextprint()continues on the same line. The last three calls build one line:Loading... done!.
sep and end must be written with their names (sep=...), after the values. For anything more complex, use an f-string from Lesson 7.
Reading input with input()
input() lets the user type something while the program runs. The text inside the brackets is the prompt: the question the user sees.
name = input("What is your name? ")
print(f"Hello, {name}!")
Here is a real run in a terminal. The program printed the prompt and waited; we typed Ayesha and pressed Enter. (On Windows, type python or py instead of python3, as shown in Lesson 3.)
$ python3 step3_input_name.py
What is your name? Ayesha
Hello, Ayesha!
The documentation explains each step: the prompt “is written to standard output without a trailing newline”, then the function “reads a line from input, converts it to a string (stripping a trailing newline), and returns that”. In simple words:
- The prompt has no new line after it, so the user types on the same line. That is why we end the prompt with a space:
"What is your name? ". - The program stops and waits until the user presses Enter.
- The Enter key itself is removed, so
nameis just"Ayesha".
Input from a pipe
You can also send input to a program from another command, which is useful for testing. Then nothing is typed on screen, so the answer does not appear after the prompt:
$ printf 'Ayesha\n' | python3 step3_input_name.py
What is your name? Hello, Ayesha!
Python still read Ayesha; it is just not shown, because no one typed it. If there is no input at all, input() cannot read a line and raises EOFError:
$ python3 step3_input_name.py < /dev/null
What is your name? Traceback (most recent call last):
File "/home/learner/python-course/step3_input_name.py", line 1, in <module>
name = input("What is your name? ")
EOFError: EOF when reading a line
EOF means “end of file”. The exceptions page says EOFError is raised when input() “hits an end-of-file condition (EOF) without reading any data”. You may see this error in tools that run code without a keyboard; run the script in a terminal instead.
input() always returns a string
This is the most important rule of the lesson. Whatever the user types, input() gives you text.
age = input("How old are you? ")
print(type(age))
print("Twice your age:", age * 2)
$ python3 step4_input_is_str.py
How old are you? 21
<class 'str'>
Twice your age: 2121
The user typed the number 21, but age is the string "21". "21" * 2 repeats the text (Lesson 7), so it prints 2121, not 42. Adding a number fails completely:
age = input("How old are you? ")
print("Next year you will be", age + 1)
$ python3 err1_input_plus_one.py
How old are you? 21
Traceback (most recent call last):
File "/home/learner/python-course/err1_input_plus_one.py", line 2, in <module>
print("Next year you will be", age + 1)
~~~~^~~
TypeError: can only concatenate str (not "int") to str
Converting input to numbers
Wrap input() in int() or float() to get a number. Python runs the inner function first: input() returns the text, then int() converts it.
age = int(input("How old are you? "))
height = float(input("Your height in metres: "))
print(f"Next year you will be {age + 1}.")
print(f"Your height in centimetres: {height * 100:.0f}")
$ python3 step5_convert.py
How old are you? 21
Your height in metres: 1.62
Next year you will be 22.
Your height in centimetres: 162
Use int() for whole numbers like age or quantity, and float() for numbers that can have a decimal point. Spaces around the number are fine, because int() and float() ignore them.
When the user types something else
Users do not always type what you expect. If the text is not a number, the conversion stops the program with a ValueError, just as in Lesson 8:
$ python3 step5_convert.py
How old are you? twenty
Traceback (most recent call last):
File "/home/learner/python-course/step5_convert.py", line 1, in <module>
age = int(input("How old are you? "))
ValueError: invalid literal for int() with base 10: 'twenty'
Pressing Enter without typing anything gives an empty string, and that fails too:
$ python3 step5_convert.py
How old are you?
Traceback (most recent call last):
File "/home/learner/python-course/step5_convert.py", line 1, in <module>
age = int(input("How old are you? "))
ValueError: invalid literal for int() with base 10: ''
For now, type valid numbers when you test. In Lesson 13 you will learn loops that can ask again, and in Lesson 35 you will learn try and except, which catch the error so the program does not crash.
A small interactive program: bill splitter
Now we put everything together. This program asks four questions, calculates a tip and each person’s share, and prints a neat summary with f-string format specs from Lesson 7.
print("=== Bill splitter ===")
place = input("Restaurant name: ")
bill = float(input("Total bill in rupees: "))
friends = int(input("Number of friends: "))
tip_percent = int(input("Tip percent: "))
tip = bill * tip_percent / 100
total = bill + tip
share = total / friends
print()
print(f"{place:^28}")
print("-" * 28)
print(f"{'Bill':<12}{bill:>16,.2f}")
print(f"{'Tip':<12}{tip:>16,.2f}")
print(f"{'Total':<12}{total:>16,.2f}")
print("-" * 28)
print(f"Each of {friends} friends pays {share:,.2f} rupees.")
$ python3 step6_bill_splitter.py
=== Bill splitter ===
Restaurant name: Karachi Grill
Total bill in rupees: 2450.50
Number of friends: 3
Tip percent: 10
Karachi Grill
----------------------------
Bill 2,450.50
Tip 245.05
Total 2,695.55
----------------------------
Each of 3 friends pays 898.52 rupees.
Notice how each input gets the type it needs: the restaurant name stays a string, the bill is a float, and the number of friends and the tip are int. The program works in three parts, which is a good shape for most small programs: input (ask), processing (calculate), output (show). For real accounts, use Decimal from Lesson 6 instead of float; since input() already returns a string, Decimal(input("Total bill: ")) works directly.
Try it yourself
Exercise 1: Name and city
Ask for the user’s name and city with two input() calls. Print them in one line like Sara from Multan, using sep instead of an f-string.
Exercise 2: Fix the adding machine
This program should add two numbers, but when you type 12 and 30 it prints 12 + 30 = 1230. Fix it.
first = input("First number: ")
second = input("Second number: ")
print(first, "+", second, "=", first + second)
Exercise 3: Temperature converter
Ask for a temperature in Celsius, convert it with fahrenheit = celsius * 9 / 5 + 32, and print the result with one decimal place.
Solutions
Solution 1
name = input("Your name: ")
city = input("Your city: ")
print(name, city, sep=" from ")
$ python3 solution1_from.py
Your name: Sara
Your city: Multan
Sara from Multan
sep=" from " is placed between the two values instead of the usual space.
Solution 2
first = int(input("First number: "))
second = int(input("Second number: "))
print(first, "+", second, "=", first + second)
$ python3 solution2_sum.py
First number: 12
Second number: 30
12 + 30 = 42
The original joined two strings, "12" + "30"; converting with int() makes + add numbers.
Solution 3
celsius = float(input("Temperature in Celsius: "))
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius} °C is {fahrenheit:.1f} °F")
$ python3 solution3_temperature.py
Temperature in Celsius: 36.6
36.6 °C is 97.9 °F
float() accepts a decimal like 36.6, and :.1f shows one decimal place.
Common mistakes
- Doing maths with the raw input.
age + 1givesTypeError: can only concatenate str (not "int") to str, andfirst + secondquietly joins text (1230). Convert withint()orfloat()first. - Using
int()for decimal input. If the user types1.62,int(input(...))givesValueError: invalid literal for int() with base 10: '1.62'. Usefloat()when a decimal point is possible. - Forgetting the space at the end of the prompt.
input("Name:")makes the answer touch the colon, likeName:Sara. Writeinput("Name: "). - Running input() where nothing can be typed. With no keyboard or input, you get
EOFError: EOF when reading a line. Run the script in a terminal. - Writing
seporendwithout their names.print("a", "b", "-")printsa b -, because"-"is just a third value. Writesep="-".
Frequently asked questions
How do I take integer input in Python?
Wrap input() in int(): age = int(input("Age: ")). input() always returns a string, and int() turns that string into a whole number. If the user types something that is not a whole number, int() raises a ValueError.
How do I print without a new line in Python?
Pass end="" to print(), for example print("Loading", end=""). The next print() then continues on the same line. You can also use another ending, such as end=" " for a space.
Why does input() return a string in Python?
Python cannot know whether you want text or a number, so input() returns exactly the characters that were typed. A name, a phone number with a leading zero and an age all arrive the same way, and you decide how to convert them.
Summary
print()separates values withsep(a space by default) and finishes withend(a new line by default).input("prompt ")shows the prompt, waits for Enter and returns the typed text without the new line.input()always returns astr; convert withint()orfloat()before doing maths.- Text that is not a number gives a
ValueError, and missing input gives anEOFError. - Small programs follow the shape input, processing, output.
Related lessons: Python strings and f-strings, Python type conversion, Python while loops.
Next lesson
Next: Lesson 11, Python if, elif and else Statements (and match).
Sources
- Built-in Functions: input() and print() (Python docs)
- Input and Output (Python tutorial)
- An Informal Introduction to Python: print() and the end argument (Python tutorial)
- Built-in Exceptions: EOFError (Python docs)
- Python downloads page (python.org)





