The print() function in Python is used to display output on the screen. It allows you to print text, values, and results, making it an essential tool for debugging and program output.
Printing Python variables helps you verify data, track program flow, and understand how your code behaves.
Note: To see how printing fits alongside other variable operations such as declaration, assignment, and deletion, refer to the Python Variables Roadmap for a clear, step-by-step learning path for Python variables.
Basic Syntax:
print(variable_name)
1. Print a Single Python Variable
Any variable you define can be passed to print() to display its current value.
Example:
message = "Hello, Python!" print(message)Output:
Hello, Python!
Explanation:
- message is a variable that stores the string “Hello, Python!”.
- Using print(message) displays its value on the screen.
2. Print Multiple Python Variables
In Python, you can print multiple variables together by separating them with commas (,) inside the print() function. By default, Python automatically adds a space between each value, making the output clean and easy to read.
Example:
name = "Alice" age = 25 print(name, age) Output: Alice 25
Explanation:
- name stores a string value “Alice”.
- age stores a numeric value 25.
- Using print(name, age) displays both values in one line, separated by a space.
3. Print Variables with Labels in Python
When printing variables, it’s often helpful to include descriptive labels so the output is easier to understand. This is especially useful when displaying multiple results or debugging your program.
In Python, you can add text (a string) alongside variables within the print() function — simply separate them with a comma ,.
Example:
score = 90
print("Student Score:", score)
Output:
Student Score: 90
Explanation:
- The string “Student Score:” acts as a label to describe what the value represents.
- The variable score holds the numeric value 90.
- Python automatically inserts a space between the label and the variable when printed together.
Tip:
Adding labels improves the readability of your output, making it more meaningful when tracking values or presenting results.
4. Print Using Python f-Strings
f-Strings (formatted string literals) are one of Python’s most convenient and modern ways to print variables within text. Introduced in Python 3.6, f-strings let you embed variables directly inside string literals without using concatenation or commas.
They make your code cleaner, faster, and easier to read — especially when printing multiple variables together.
Example:
name = "Bob"
marks = 88
print(f"Student {name} scored {marks} marks.")
#Output:
Student Bob scored 88 marks.
Explanation:
- The prefix f before the string allows embedding expressions or variables inside {}.
- {name} and {marks} are replaced with their actual values when printed.
- This approach improves readability compared to older string formatting methods.
Tip:
Use f-strings whenever possible for neat and readable code. They also support expressions, so you can do things like:
print(f"Total marks: {marks + 12}")
5. Print with String Concatenation in Python
String concatenation in Python allows combining text and variables using the + operator. This method is common in beginner-friendly examples and helps build simple, readable print statements. However, it requires that all parts being joined are strings — otherwise, you’ll get a type error.
Example:
course = "Python"
duration = "6 weeks"
print("Course: " + course + ", Duration: " + duration)
Output:
Course: Python, Duration: 6 weeks
Explanation:
- The + operator joins multiple string values together.
- Here, “Course: “, course, “, Duration: “, and duration are merged into one final string.
- It’s essential that all variables are string types before concatenation.
Tip: If you’re working with numbers, convert them using str() before concatenating:
age = 25
print("Age: " + str(age))
This avoids errors and ensures smooth output.
Best Practice:
While concatenation works fine, f-strings or format() are cleaner and more efficient for larger or dynamic print statements.6. Print Python Variables with Type Conversion
When printing variables in Python, all parts of the statement must be string-compatible. If a variable holds a non-string value (like an integer or float), you’ll need to convert it using type conversion functions such as str(), int(), or float(). This ensures your print statements run smoothly without errors.
Example:
age = 30
print("Age: " + str(age)) # Converts integer to string before printing
Output:
Age: 30
Explanation:
- The variable age stores an integer value.
- Using str(age) converts the integer to a string so it can be concatenated safely with “Age: “.
- Without conversion, Python would raise a TypeError because it can’t directly join strings and numbers.
Tip: For cleaner output, consider using f-strings instead of manual conversion:
print(f"Age: {age}")
F-strings automatically handle type conversion, making your code shorter and more readable.
7. Print with sep and end in Python
The print() function in Python isn’t limited to basic output—it includes two powerful optional parameters: sep and end, which make your printed output more flexible and professional.
Parameter Description
sep Defines the separator between multiple values. (Default: space ” “)
end Defines what is printed at the end of the statement. (Default: newline “\n”)
These parameters are especially useful when formatting outputs such as dates, file paths, or progress indicators.
i) Example with sep Parameter
day = "12" month = "05" year = "2025" print(day, month, year, sep="-") Output: 12-05-2025
Explanation:
<>The sep=”-” argument replaces the default space separator with a dash (-), making it ideal for formatting structured data like dates or version numbers.ii) Example with end Parameter
print("Start", end="...")
print("End")
Output:
Start...End
Explanation:
Here, end=”…” ensures the next print() statement continues on the same line, separated by three dots instead of a newline. It’s often used for progress indicators or inline messages.
Tip: Combine sep and end to achieve custom and dynamic output formatting in one line.
print("Loading", "Complete", sep=" → ", end="!\n")
Output:
Loading → Complete!
8. Print Python Variables in Loops
Printing variables inside loops is one of the most common ways to track progress, debug values, or monitor iterations in Python. The print() function is often used in for or while loops to display changing values at each iteration. This helps in understanding how your loop progresses and what data it processes.
Example: Printing Values Inside a Loop
for i in range(3):
print("Index:", i)
Output:
Index: 0
Index: 1
Index: 2
Explanation:
In this example, the loop runs three times — for values 0, 1, and 2.At each iteration, the print() statement displays the current value of i along with a descriptive label.
Tip:
You can enhance loop outputs with f-strings for cleaner formatting:
for i in range(3):
print(f"Iteration number → {i}")
Output:
Iteration number → 0 Iteration number → 1 Iteration number → 2
Explanation:
Using an f-string makes the output easier to read and more expressive, which is especially helpful when printing complex variables or debugging logic.
9. Print Using Python format() Method
The .format() method in Python provides a flexible way to insert variables inside strings. It works by placing curly braces {} as placeholders, which are later replaced by the values you specify inside .format(). This method was widely used before f-strings were introduced in Python 3.6 and still remains useful for compatibility with older versions.
Example: Using .format() to Print Variables
name = "Diana"
score = 96
print("Student {} scored {}".format(name, score))
Output:
Student Diana scored 96
Explanation:
Here, the placeholders {} inside the string are replaced in order by the values provided in .format().
So, the first {} becomes “Diana” and the second {} becomes 96.
Tip: Specify Placeholder Positions or Names
Example (Positional):
print("Student {0} scored {1}".format("Alice", 85))
Example (Named):
print("Student {name} scored {marks}".format(name="Bob", marks=90))
Output:
Student Alice scored 85
Student Bob scored 90
Explanation:
Using numbered {0}, {1} or named placeholders like {name} improves readability and gives better control over variable order — a great practice in formatted text generation.
10. Summary Table: Techniques for Printing Python Variables
| Technique | Syntax Example | Description / Notes |
|---|---|---|
| Basic Print | print(x) | Displays the value of a single variable. |
| Multiple Variables | print(x, y) | Prints multiple items separated by a space. |
| Descriptive Labels | print(“Name:”, name) | Combines text with variable values for clarity. |
| f-String (Recommended) | print(f”Score: {score}”) | Best modern approach in Python 3.6+ for readability. |
| String Concatenation | “A” + “B” | Joins strings; all parts must be str type. |
| .format() Method | “{} {}”.format(a, b) | Flexible method; supports older Python versions. |
| Separator (sep) | print(a, b, sep=”-“) | Defines custom separator between printed items. |
| End (end) | print(a, end=”*”) | Prevents automatic newline or adds a custom ending. |