The print() function displays variable values in the console. The simplest form is print(variable). When you need text with variables, f-strings are usually the cleanest option in modern Python.
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
Quick answer: print a variable in Python
Use print(name) to print one variable. Use print("Name:", name) to print text and a variable. Use print(f"Name: {name}") for clean formatted output.
name = "Deepak"
age = 30
print(name)
print("Name:", name)
print(f"Name: {name}, Age: {age}")The first line prints Deepak. The second prints Name: Deepak with a space after the colon. The third prints both values inside one formatted sentence.
Python print variable quick reference
| Task | Use |
|---|---|
| Print one variable | print(name) |
| Print multiple variables | print(name, age) |
| Print text and variable | print("Name:", name) |
| Print variable inside text | print(f"Name: {name}") |
| Print number with text | print(f"Age: {age}") |
| Print float with 2 decimals | print(f"Price: {price:.2f}") |
| Print without spaces between values | print(a, b, sep="") |
| Print without newline | print(value, end="") |
| Print variable type | print(type(value)) |
| Store formatted text in variable | message = f"Name: {name}" |
| Print list or dict clearly | pprint(data) |
| Print to file | print(value, file=file_object) |
Print a variable in Python
Use print(variable). The variable name must not be inside quotes.
name = "Deepak"
print(name)
print("name")The first line prints Deepak. The second prints the literal text name because it is a string, not a variable reference.
Print text and variable together
Pass multiple values to print(). Python adds a space between them by default.
name = "Deepak"
age = 30
print("Name:", name)
print("Age:", age)This works with strings and numbers without calling str() first. The print() documentation explains how sep controls the separator between objects.
Print variable using f-string
Put f before the string and place variables inside {}. F-strings are readable and work well for strings, numbers, expressions, and formatting.
name = "Deepak"
age = 30
print(f"My name is {name}")
print(f"Next year I will be {age + 1}")F-strings are the best default for new Python code when you want text mixed with variable values. See Python string format for deeper formatting options.
Print multiple variables
Use comma-separated values in one print() call. Use sep when you need a custom separator.
name = "Deepak"
age = 30
city = "Delhi"
print(name, age, city)
print(name, age, city, sep=", ")The first line uses the default space separator. The second prints comma-separated values on one line.
Print string and number variable
Comma syntax and f-strings handle mixed types safely. String concatenation with + does not.
age = 30
print("Age:", age)
print(f"Age: {age}")
print("Age: " + str(age))The first two lines work without conversion. The third works only because str(age) converts the integer to a string before concatenation.
This line would fail:
# print("Age: " + age) # TypeErrorPrint variable using + concatenation
The + operator joins strings. Every non-string value needs str() first.
first = 5
second = 10
print("The sum of " + str(first) + " and " + str(second) + " is " + str(first + second))This works, but f-strings or comma syntax are usually easier for beginners printing mixed types.
Print variable using format()
Use str.format() when you need reusable templates or when reading older code.
name = "Deepak"
age = 30
print("Name: {}".format(name))
print("Name: {}, Age: {}".format(name, age))For Python 3.6 and later, f-strings are usually simpler for the same result.
Print variable using % formatting
Old-style % formatting still appears in legacy code.
name = "Deepak"
age = 30
price = 19.99
print("Name: %s" % name)
print("Age: %d" % age)
print("Price: %.2f" % price)Use %s for strings, %d for integers, and %.2f for floats with two decimal places. Prefer f-strings in new code.
Print float or decimal with formatting
Formatting controls display only. It does not change the stored numeric value.
price = 19.9876
print(f"Price: {price:.2f}")
print("Price: {:.2f}".format(price))Both lines display 19.99 (rounded for display). For more formatting patterns, see Python string format.
Print without newline
By default, print() ends with a newline. Use end="" to stay on the same line.
print("Loading", end="")
print(".", end="")
print(".", end="")
print(" done")All four calls can appear on one line in the terminal: Loading... done.
Print without spaces between variables
Use sep="" to remove the default space between values.
part1 = "Hello"
part2 = "World"
print(part1, part2)
print(part1, part2, sep="")
print(part1, part2, sep=", ")The first call prints Hello World with a space. The second prints HelloWorld. The third prints Hello, World.
Print variable to a file
Pass a file object with the file parameter. Open the file in text mode first.
from pathlib import Path
path = Path("output.txt")
name = "Deepak"
with path.open("w", encoding="utf-8") as f:
print(name, file=f)This writes the same text print() would show on screen. For broader file patterns, see Python write to file.
Print to variable vs store value in variable
print() displays output. It does not return the printed text. Assign a formatted string when you need to store it.
name = "Deepak"
message = f"Name: {name}"
print(message)
result = print(name)
print(result)message holds the formatted string. result is None because print() returns nothing useful.
Pretty print variables
Use pprint() from the pprint module for nested lists and dictionaries.
from pprint import pprint
data = {
"users": [
{"name": "Deepak", "roles": ["admin", "editor"]},
{"name": "Amit", "roles": ["viewer"]},
]
}
print(data)
pprint(data)pprint() adds line breaks and indentation so nested data is easier to read during debugging.
Summary
Use print(variable) for one value. Use print("Text:", variable) for simple text plus a variable. Use f-strings for clean formatted output. Use sep and end to control spacing and newlines. Use pprint() for nested data. Assign formatted text to a variable when you want to store it instead of printing immediately.

