Printing Python Variables: Complete Guide with Practical Examples

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)

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!

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

TechniqueSyntax ExampleDescription / Notes
Basic Printprint(x)Displays the value of a single variable.
Multiple Variablesprint(x, y)Prints multiple items separated by a space.
Descriptive Labelsprint(“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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top