Print variable and a string in python
Categories:
Printing Variables and Strings in Python: A Comprehensive Guide

Learn the fundamental techniques for combining variables and string literals in Python for clear and informative output, from basic concatenation to f-strings.
Printing information to the console is a cornerstone of programming, essential for debugging, providing user feedback, and displaying results. In Python, the print()
function is your primary tool for this. This article will guide you through various methods to effectively combine variables with static string text, ensuring your output is both readable and dynamic.
Basic Printing with the print()
Function
The print()
function can accept multiple arguments, separated by commas. By default, it will print each argument separated by a single space. This is the simplest way to combine a variable's value with a string.
name = "Alice"
age = 30
print("Hello,", name, "! You are", age, "years old.")
Using commas to separate strings and variables in print()
While straightforward, this method automatically adds spaces between items. If you need more control over spacing or want to avoid extra spaces, other methods are more suitable.
String Concatenation with the +
Operator
The +
operator can be used to concatenate (join) strings. When combining variables with strings using +
, remember that all elements must be strings. If your variable is a number, you'll need to explicitly convert it to a string using str()
.
city = "New York"
temperature = 25 # an integer
# Correct: Convert temperature to string
print("The city is " + city + " and the temperature is " + str(temperature) + "°C.")
# Incorrect: This would raise a TypeError
# print("The city is " + city + " and the temperature is " + temperature + "°C.")
Concatenating strings and variables using the +
operator
+
for concatenation. If you try to concatenate a string with a non-string type (like an integer or float) without converting it first, Python will raise a TypeError
.Formatted String Literals (f-strings)
Introduced in Python 3.6, f-strings (formatted string literals) provide a concise and readable way to embed expressions inside string literals. They are prefixed with f
or F
and use curly braces {}
to contain expressions that will be evaluated and formatted into the string.
product = "Laptop"
price = 1200.50
quantity = 2
print(f"You purchased {quantity} {product}(s) for a total of ${price * quantity:.2f}.")
# You can also include expressions directly
print(f"Next year, {product} might cost ${price * 1.1:.2f}.")
Using f-strings for dynamic and readable output
:.2f
) or padding.The .format()
Method
Before f-strings, the .format()
method was the preferred way to format strings. It uses curly braces {}
as placeholders within a string, and the values to be inserted are passed as arguments to the .format()
method.
item = "Book"
author = "Jane Doe"
year = 2023
# Positional arguments
print("The {} by {} was published in {}.".format(item, author, year))
# Keyword arguments
print("The {product} by {writer} was published in {pub_year}.".format(product=item, writer=author, pub_year=year))
# Index-based arguments
print("The {0} by {1} was published in {2}.".format(item, author, year))
String formatting using the .format()
method
flowchart TD A[Start] B{Choose Printing Method} C[print() with commas] D[String Concatenation (+)] E[f-strings] F[.format() Method] G[Output to Console] A --> B B --> C B --> D B --> E B --> F C --> G D --> G E --> G F --> G subgraph Considerations C1[Automatic Spacing] C2[Type Conversion Needed] C3[Readability & Performance] C4[Flexibility] end C -- "Adds spaces" --> C1 D -- "Requires str()" --> C2 E -- "Modern, Fast" --> C3 F -- "Good for older Python" --> C4
Decision flow for choosing a Python printing method