Print Variable in Python

Learn how to print a variable in Python using print(), f-strings, commas, str.format(), and % formatting. See examples for printing strings, numbers, multiple variables, text with variables, and avoiding common TypeError mistakes.

Published

Updated

Tech reviewed byDeepak Prasad

Print Variable in Python

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.

python
name = "Deepak"
age = 30

print(name)
print("Name:", name)
print(f"Name: {name}, Age: {age}")
Output

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)

Use print(variable). The variable name must not be inside quotes.

python
name = "Deepak"

print(name)
print("name")
Output

The first line prints Deepak. The second prints the literal text name because it is a string, not a variable reference.


Pass multiple values to print(). Python adds a space between them by default.

python
name = "Deepak"
age = 30

print("Name:", name)
print("Age:", age)
Output

This works with strings and numbers without calling str() first. The print() documentation explains how sep controls the separator between objects.


Put f before the string and place variables inside {}. F-strings are readable and work well for strings, numbers, expressions, and formatting.

python
name = "Deepak"
age = 30

print(f"My name is {name}")
print(f"Next year I will be {age + 1}")
Output

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.


Use comma-separated values in one print() call. Use sep when you need a custom separator.

python
name = "Deepak"
age = 30
city = "Delhi"

print(name, age, city)
print(name, age, city, sep=", ")
Output

The first line uses the default space separator. The second prints comma-separated values on one line.


Comma syntax and f-strings handle mixed types safely. String concatenation with + does not.

python
age = 30

print("Age:", age)
print(f"Age: {age}")
print("Age: " + str(age))
Output

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:

python
# print("Age: " + age)  # TypeError
Output

The + operator joins strings. Every non-string value needs str() first.

python
first = 5
second = 10

print("The sum of " + str(first) + " and " + str(second) + " is " + str(first + second))
Output

This works, but f-strings or comma syntax are usually easier for beginners printing mixed types.


Use str.format() when you need reusable templates or when reading older code.

python
name = "Deepak"
age = 30

print("Name: {}".format(name))
print("Name: {}, Age: {}".format(name, age))
Output

For Python 3.6 and later, f-strings are usually simpler for the same result.


Old-style % formatting still appears in legacy code.

python
name = "Deepak"
age = 30
price = 19.99

print("Name: %s" % name)
print("Age: %d" % age)
print("Price: %.2f" % price)
Output

Use %s for strings, %d for integers, and %.2f for floats with two decimal places. Prefer f-strings in new code.


Formatting controls display only. It does not change the stored numeric value.

python
price = 19.9876

print(f"Price: {price:.2f}")
print("Price: {:.2f}".format(price))
Output

Both lines display 19.99 (rounded for display). For more formatting patterns, see Python string format.


By default, print() ends with a newline. Use end="" to stay on the same line.

python
print("Loading", end="")
print(".", end="")
print(".", end="")
print(" done")
Output

All four calls can appear on one line in the terminal: Loading... done.


Use sep="" to remove the default space between values.

python
part1 = "Hello"
part2 = "World"

print(part1, part2)
print(part1, part2, sep="")
print(part1, part2, sep=", ")
Output

The first call prints Hello World with a space. The second prints HelloWorld. The third prints Hello, World.


Pass a file object with the file parameter. Open the file in text mode first.

python
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() displays output. It does not return the printed text. Assign a formatted string when you need to store it.

python
name = "Deepak"

message = f"Name: {name}"
print(message)

result = print(name)
print(result)
Output

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.

python
from pprint import pprint

data = {
    "users": [
        {"name": "Deepak", "roles": ["admin", "editor"]},
        {"name": "Amit", "roles": ["viewer"]},
    ]
}

print(data)
pprint(data)
Output

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.


References


Frequently Asked Questions

1. How do you print a variable in Python?

Use print(variable) to display the value. The variable name must be outside quotes. print(name) shows the value; print("name") prints the word name as text.

2. What is the easiest way to print text and a variable together?

Use print("Text:", variable) for simple output, or print(f"Text: {variable}") with an f-string for formatted text in modern Python.

3. Can you print a string and an integer with + in Python?

Not directly. "Age: " + age raises TypeError when age is an int. Use print("Age:", age), an f-string, or convert the number with str(age) before using +.

4. Does print() store output in a variable?

No. print() displays output and returns None. To store formatted text, assign it first, for example message = f"Name: {name}".

5. How do you print without a newline in Python?

Use print(value, end="") to keep the next print() on the same line. By default, print() adds a newline at the end.

6. How do you print multiple variables on one line?

Use print(a, b, c). Python adds a space between values by default. Control spacing with sep, for example print(a, b, sep=", ").
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …