Search Here

Python – Output Variables

To output a variable in Python, you can use the print() function. Here is an example:

name = "John"
print(name)  # Output: John

You can also output multiple variables by separating them with a comma. For example:

x = 1
y = 2
z = 3
print(x, y, z)  # Output: 1 2 3

You can also use the format() method to output variables in a string. For example:

name = "John"
age = 30
print("My name is {} and I am {} years old.".format(name, age))  # Output: My name is John and I am 30 years old.

The curly braces {} are placeholders for the values that will be passed to the format() method. The values are passed in the order in which the placeholders appear in the string.

You can also use the f-string feature (available in Python 3.6 and above) to output variables in a string. For example:

name = "John"
age = 30
print(f"My name is {name} and I am {age} years old.")  # Output: My name is John and I am 30 years old.

The f before the string indicates that it is an f-string, and the expressions inside the curly braces are evaluated and their values are inserted into the string.

Leave a Comment