User Input & Output
Learn how to display formatted output with print() and collect user input with input(), including converting input to numeric types.
Introduction
A program that cannot talk to its user is not very useful. Python gives you two essential tools for this: print(), which displays output, and input(), which collects text typed by the user. Together, they let you build programs that actually interact with a person.
You have already used print() briefly in earlier lessons. This lesson goes much deeper — covering its optional arguments, how input() behaves, and how to combine the two into a small interactive program.
- How to print multiple values, and control spacing with sep and end.
- How input() works and why it always returns a string.
- How to convert input into numbers using int() and float().
- How to build a simple interactive program combining input, processing, and output.
The print() Function
print() displays whatever you give it on the screen, followed by a new line by default.
print("Welcome to Python!")Welcome to Python!Printing Multiple Values
print() can accept several values at once, separated by commas. It automatically converts each one to text and joins them with a single space by default.
name = "Aarav"
age = 17
grade = "A"
print("Name:", name, "Age:", age, "Grade:", grade)Name: Aarav Age: 17 Grade: ANotice that print() automatically handled turning the int 17 into displayable text — you did not need to call str() yourself.
The sep Parameter
By default, print() separates multiple values with a single space. The sep parameter lets you change that separator to anything you like.
print("2026", "07", "25", sep="-")
print("apple", "banana", "cherry", sep=", ")
print("a", "b", "c", sep="")2026-07-25
apple, banana, cherry
abcThe end Parameter
By default, print() ends with a newline character, which is why each call to print() normally appears on its own line. The end parameter lets you replace that newline with something else, such as a space, so multiple print() calls appear on the same line.
print("Line one")
print("Line two")Line one
Line twoprint("Loading", end="")
print(".", end="")
print(".", end="")
print(".")Loading...The input() Function
input() pauses your program and waits for the user to type something and press Enter. You can pass it an optional message, called a prompt, which is displayed before the user types.
name = input("What is your name? ")
print("Hello,", name)What is your name? Meera
Hello, Meerainput() Always Returns a String
No matter what the user types — even if it looks like a number — input() always returns a str. If you need to do math with the result, you must explicitly cast it using int() or float().
age = input("Enter your age: ")
print(type(age))Enter your age: 16
<class 'str'>age = int(input("Enter your age: "))
next_year_age = age + 1
print("Next year you will be", next_year_age)Enter your age: 16
Next year you will be 17If you skip int() and try age + 1 while age is still a string, Python raises a TypeError, because you cannot add an int to a str.
Building an Interactive Program
Let's combine everything from this lesson — taking input, converting it, doing a calculation, and printing a formatted result.
name = input("What is your name? ")
price = float(input("Enter the item price: "))
quantity = int(input("Enter the quantity: "))
total = price * quantity
print("Hello,", name)
print("Item price:", price)
print("Quantity:", quantity)
print("Total cost:", total)What is your name? Kabir
Enter the item price: 250
Enter the quantity: 3
Hello, Kabir
Item price: 250.0
Quantity: 3
Total cost: 750.0A Preview of Output Formatting
So far, print() has combined values with commas and spaces, which works but can feel clunky for longer messages. Python has a much cleaner way to build formatted strings called f-strings, which let you embed variables directly inside text.
name = "Kabir"
total = 750.0
print(f"Thank you, {name}! Your total is {total}.")Thank you, Kabir! Your total is 750.0.This is just a preview — f-strings get a full dedicated lesson later, covering number formatting, alignment, and much more.
Common Mistakes
- Forgetting that input() always returns a string, then getting a TypeError when doing math.
- Casting input to int() when the value might contain a decimal point — use float() instead.
- Assuming print() adds commas between values instead of spaces by default.
- Forgetting that print() ends with a newline unless you set end to something else.
- Not including a clear prompt message inside input(), leaving the user unsure what to type.
Best Practices
- Always write a clear, specific prompt inside input(), like input("Enter your age: ").
- Cast input immediately to the type you need, right where you read it.
- Use float() instead of int() whenever the input might include a decimal point.
- Use sep and end to fine-tune formatting instead of manually concatenating strings.
- Prefer f-strings over comma-separated print() arguments once you are comfortable with them.
Frequently Asked Questions
Does input() ever return a number?
No. input() always returns a str, even if the user types digits. You must convert it yourself using int() or float() if you need to do math.
What happens if the user types letters when I expect a number?
Calling int() or float() on non-numeric text raises a ValueError. Handling this gracefully requires exception handling, covered in a later lesson.
What is the default separator in print()?
A single space. You can change it with the sep parameter, e.g. print("a", "b", sep="-").
What is the default value of end in print()?
A newline character, "\n", which is why each print() call normally starts on a new line.
Should I use f-strings instead of commas in print()?
Either works. Commas in print() are simple and fine for quick output; f-strings (covered in the next lessons) give you more control over formatting as your messages grow more complex.
Key Takeaways
- print() displays output and automatically converts values to text.
- The sep parameter controls what appears between multiple printed values.
- The end parameter controls what print() adds after the output, instead of the default newline.
- input() displays an optional prompt and always returns the user's response as a str.
- You must explicitly cast input() results with int() or float() before doing math with them.
Summary
print() and input() are the two functions that let your Python programs communicate with the person using them. print() gives you fine control over formatting through sep and end, while input() always hands back a string that you must cast yourself when you need a number.
You combined both functions into a small interactive program that reads values, does a calculation, and displays a formatted result. Next, you will start a deep dive into one of Python's most-used data types: strings.
- You can use print() with sep and end for precise formatting.
- You understand that input() always returns a string.
- You can build a simple interactive program using input, casting, and output.
- You are ready to begin a deep dive into strings.