To learn how to print variables in Python, start with the built-in print() function. It displays a value in your program’s output, adds a newline by default, and can show strings, numbers, lists, and other objects.
The basic way to print a variable in Python is to pass its name to print(). Python evaluates the variable and displays its current value.
How do you print a variable in Python?
Assign a value to a variable, then provide that variable as the argument to print():
name = ‘Mina’; print(name)
Output: Mina
This works with numeric values as well:
age = 29; print(age)
Output: 29
When the variable contains a string, print() displays the characters without quotation marks. For numbers, it displays the numeric value directly. You do not need to convert either value before passing it to print().
How do you print labels and several values?
Pass several arguments to print(), separated by commas. Python converts each value for display and places a space between arguments by default:
name = ‘Mina’; age = 29; print(‘Name:’, name, ‘Age:’, age)
Output: Name: Mina Age: 29
This approach is useful for quick status messages because it safely displays text and non-string values together. It also avoids trying to combine text and numbers with an incompatible string operation.
Use the sep parameter to replace the default space between arguments:
print(‘2025′, ’03’, ’08’, sep=’-‘)
Output: 2025-03-08
The end parameter controls what follows the printed value. Its default is a newline. Set it to another string when output should continue on the same line:
print(‘Loading’, end=’…’); print(‘done’)
Output: Loading…done
How does printing variables in Python work with f-strings?
F-strings place variable values inside a text template. Add the letter f before the opening quote, then put each variable or expression inside braces:
name = ‘Mina’; score = 94; print(f'{name} scored {score} points.’)
Output: Mina scored 94 points.
F-strings are usually the clearest choice when a message contains labels and several values. They also support conversion and formatting specifiers. Add a colon inside the braces to format a number:
price = 19.5; print(f’Price: ${price:.2f}’)
Output: Price: $19.50
Here, :.2f displays a floating-point number with two digits after the decimal point. Use a comma to group large numbers:
total = 1234567; print(f'{total:,}’)
Output: 1,234,567
Expressions can also appear inside an f-string:
items = 3; price = 4.5; print(f’Total: ${items * price:.2f}’)
Output: Total: $13.50
How do str and repr help when debugging?
For ordinary output, print() uses a value’s human-readable string form, called str. This keeps displayed text easy to read. The repr form is designed for debugging and aims to reveal the value more precisely.
The difference is especially visible with strings containing escape characters:
message = ‘A\nB’; print(message)
This displays A and B on separate lines. By contrast:
print(repr(message))
Output: ‘A\nB’
The repr() result shows quotation marks and the literal escape sequence, making hidden characters easier to identify. It is also useful for distinguishing a string from a number or spotting extra spaces.
Use the !r conversion inside an f-string when you need that debug-friendly representation:
print(f’message={message!r}’)
Output: message=’A\nB’
Use !s when you explicitly want the normal string form. For routine messages, use standard interpolation or comma-separated arguments; reserve repr() and !r for inspecting values and diagnosing unexpected output.
