What Is Output for Variables in Python?
name = "Alice" age = 25 print("Name:", name, "Age:", age) # Output: # Name: Alice Age: 25 In Python, outputting variables means displaying the value stored in a variable on the screen (console). This is usually done using the built-in print() function.
Printing a Single Variable
Assign a value to a variable, then use print() to display that value.
x = "Python is awesome" print(x) # Output: # Python is awesomePrinting Multiple Variables
You can pass more than one variable to print() by separating them with commas.
x = "Python" y = "is" z = "awesome" print(x, y, z) # Output: # Python is awesomeUsing commas automatically inserts spaces between values.
Combining Variables and Text
1. Using Comma Separation
The simplest way to mix text and variables is by separating them with commas inside print():
name = "Alice" age = 25 print("Name:", name, "Age:", age) # Output: # Name: Alice Age: 252. Using String Concatenation
You can join strings and variables with the + operator when working with strings:
first = "Hello " last = "World!" print(first + last) # Output: # Hello World!This method only works cleanly with strings; numbers must be converted with str() first.
3. Using f-Strings (Formatted Strings)
f-strings provide a modern and readable way to include variable values within text:
name = "Alice" age = 25 print(f"Name: {name}, Age: {age}") # Output: # Name: Alice, Age: 25This works in Python 3.6 and later.
Numeric Output and Operators
When printing numeric variables, the + operator adds values mathematically rather than concatenating:
x = 5 y = 10 print(x + y) # Output: # 15If you try to concatenate a string and number directly, Python raises an error. Instead, use commas or formatting