print() / input() / repr()
These are the core built-in functions for printing output to the console, reading user input, and getting the string representation of an object.
Syntax
# Prints values to the console. print(value1, value2, ..., sep=' ', end='\n', file=sys.stdout) # Reads a line of text from the user. The return value is always a string. input(prompt) # Returns the "official" string representation of an object. repr(object) # Formats a value as a string using a format specifier. format(value, format_spec)
Function List
| Function | Description |
|---|---|
| print(*objects, sep, end, file) | Converts arguments to strings and prints them to the console. Multiple values are separated by a space, and a newline is added at the end. |
| input(prompt) | Displays a prompt, waits for the user to type a line, and returns that line as a string. |
| repr(obj) | Returns the official string representation of an object. For strings, this includes quotes, and the goal is a format that can be passed to eval() to recreate the original object. |
| format(value, format_spec) | Returns a formatted string for the given value according to the format specifier. |
Sample Code
# Basic usage of print().
print("Hello, Python!") # Hello, Python!
# Print multiple values. The default separator is a space.
name = "Alice"
age = 20
print("Name:", name, "Age:", age) # Name: Alice Age: 20
# Specify a custom separator with sep.
print(2026, 3, 5, sep="-") # 2026-3-5
# Change the ending character with end (default is a newline).
print("Loading", end="...") # Prints without a newline, so the next print continues on the same line.
print("Done!")
# Use input() to receive a string from the user.
# name = input("Enter your name: ")
# print(f"Hello, {name}!")
# The return value of input() is always a string, so convert it when you need a number.
# num_str = input("Enter a number: ")
# num = int(num_str) # Convert the string to an integer.
# Use repr() to get the official string representation.
s = "hello\nworld"
print(s) # Displays: hello (newline) world
print(repr(s)) # Displays: 'hello\nworld' — the escape sequence is shown literally.
# repr() is handy for debugging.
data = [1, "two", 3.0, None]
print(repr(data)) # [1, 'two', 3.0, None]
# Use format() to format a value as a string.
pi = 3.14159265
print(format(pi, ".2f")) # 3.14 (two decimal places)
print(format(1000000, ",")) # 1,000,000 (comma-separated)
print(format(42, "08d")) # 00000042 (8 digits, zero-padded)
print(format(255, "x")) # ff (hexadecimal)
Overview
print() is the most commonly used output function in Python. Internally, it calls str() on each argument to convert it to a string before printing. To write output to a file instead of the console, pass a file object to the file argument (e.g., file=sys.stderr writes to standard error).
The return value of input() is always a string (str). If you need a numeric value, you must convert it with int() or float(). If the input cannot be converted, a ValueError is raised, so in production code you should handle this with a try-except block.
The difference between repr() and str() lies in their intended audience. str() returns a human-readable form, while repr() returns a developer-oriented form suited for debugging. You can customize this behavior in your own classes by implementing the __repr__() method. For details on string formatting, see str.format() / f-strings.
If you find any errors or copyright issues, please contact us.