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
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
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
Enter your name: Alice
Enter your age: 25
Name: Alice Age: 25
Alice is 25 years old.
A-B-C!
Score: 95.506. 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
1. What data type does `input()` always return, regardless of what the user types?
2. What happens when you run `age = input('Age: ')` (user types 25) and then compute `age + 1`?
3. What does `print("A", "B", sep="-")` output?
4. Which of these is an f-string that embeds the variable `name`?
5. What does the format specifier `{:.2f}` do when applied to a float?
Was this page helpful?
You May Also Like
Type Casting in Python
How to convert values between Python data types explicitly with functions like int(), float(), and str(), and how implicit coercion works.
Strings in Python
Python's immutable text sequence type, covering string creation, indexing, slicing, immutability, and formatting.
Common String Methods in Python
A practical tour of Python's built-in string methods, grouped by case conversion, search/replace, and split/join, with usage examples.
Comments in Python
How to write single-line and multi-line comments in Python, and the difference between comments and docstrings.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics