100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Input and Output in Python

How to read user input with input() and display output with print(), including formatting with f-strings, sep, end, and format().

Basics & Data TypesBeginner8 min readJul 7, 2026
Analogies

1. Introduction

Almost every interactive program needs to communicate with its user: reading data they type in, and displaying results back to them. Python provides the built-in input() function to read text from the keyboard and the print() function to write text to the console.

🏏

Cricket analogy: Just as an umpire signals a decision (output) after receiving the third umpire's call via earpiece (input), a Python program uses input() to read what the user types and print() to display results back.

Beyond the basics, Python offers several ways to format output cleanly — f-strings, the str.format() method, and the sep/end keyword arguments of print() — which make it easy to produce readable, well-structured console output.

🏏

Cricket analogy: Just as a scoreboard can display '142/3 in 18.4 overs' using different formatting templates depending on the ground's display system, Python offers f-strings, str.format(), and print()'s sep/end to format output in different readable ways.

2. Syntax

python
name = input("Enter your name: ")   # always returns a str

print("Hello", name)
print("Hello", name, sep=", ", end="!\n")
print(f"Hello {name}")
print("Hello {}".format(name))

3. Explanation

input(prompt) displays the optional prompt, waits for the user to type a line of text and press Enter, and returns exactly what was typed as a string — never as a number, even if the user types digits. If you need a number, you must explicitly convert the result with int() or float().

🏏

Cricket analogy: Just as a scorer jotting down a spoken run count on paper still needs to convert it to a numeric tally before adding it to the total, input() always returns what the user typed as a string, even digits, and must be explicitly converted with int() or float().

print() accepts any number of values, converts each to its string form, and joins them with a separator (default a single space), finishing with an end string (default a newline). The sep and end keyword arguments let you customize both. F-strings (f"...{expr}...") are generally the most readable way to embed variables and expressions directly into output text.

🏏

Cricket analogy: Just as a commentator strings together the batsman's name, runs, and balls faced with a space and a full stop at the end of the sentence, print() joins its arguments with a default space separator and ends with a newline, both customizable, and f-strings are the clearest way to embed those stats.

Common gotcha: input() always returns a str, so age = input("Age: ") followed by age + 1 raises TypeError: can only concatenate str (not "int") to str. Always convert with int(input(...)) when you need numeric input.

print() flushes to standard output by default at the end of each call (because end defaults to "\n"); pass end="" to keep the cursor on the same line for the next print call.

4. Example

python
name = input("Enter your name: ")   # Assume user enters: Alice
age = int(input("Enter your age: "))  # Assume user enters: 25

print("Name:", name, "Age:", age)
print(f"{name} is {age} years old.")
print("A", "B", "C", sep="-", end="!\n")
print("Score: {:.2f}".format(95.5))

5. Output

text
Enter your name: Alice
Enter your age: 25
Name: Alice Age: 25
Alice is 25 years old.
A-B-C!
Score: 95.50

6. Key Takeaways

  • input() always returns a string; convert it explicitly with int() or float() for numeric use.
  • print() joins arguments with sep (default a space) and finishes with end (default a newline).
  • f-strings (f"{expr}") are the most concise and readable way to embed values in output.
  • str.format() and the older % formatting are alternative ways to build formatted strings.
  • Format specifiers like {:.2f} control decimal precision when printing floats.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#InputAndOutputInPython#Input#Output#Syntax#Explanation#StudyNotes#SkillVeris