Print Single and Multiple variable in Python (original) (raw)
Last Updated : 16 Apr, 2025
In Python, printing single and multiple variables refers to displaying the values stored in one or more variables using the **print() function.
Let’s look at ways how we can print variables in Python:
Printing a Single Variable in Python
The simplest form of output is displaying the value of a single variable using the **print() function.
**Example:
Python `
x = 42
print(x)
`
**Explanation: **x stores the value **42, and **print(x) displays it on the console.
Printing Multiple Variables
Python allows printing multiple variables in various ways. Let’s explore three common techniques:
1. Using Commas (default way)
Python `
a = 10 b = 20 c = 30
print(a, b, c)
`
**Explanation: When variables are separated by commas, Python prints them with a space between each.
2. Using f-strings (formatted string literals)
Python `
name = "Prajjwal" age = 23
print(f"My name is {name} and I am {age} years old.")
`
Output
My name is Prajjwal and I am 23 years old.
**Explanation:
- **f-strings are prefixed with the letter f before the opening quote.
- Inside the string, **curly braces {} act as placeholders for variables or expressions.
- **Python automatically replaces each {} with the value of the **corresponding variable.
**Note: f-strings is compatible with **latest Python **versions (**Python 3.6+).
3. Using .format() Method
Python `
lang = "Python" ver = 3
print("You are learning {} version {}.".format(lang, ver))
`
Output
You are learning Python version 3.
**Explanation:
- In the string, the curly braces ****{}** act as **placeholders.
- ****.format()** method replaces the {} in the string in order with the values passed to it.
**Note: ****.format()** is compatible with **older Python versions and still widely used in legacy code.