Multiple Variables in Python
Python allows you to work with multiple variables simultaneously by defining them in a single statement or line. This can be done using various way , here we are explaining some generally used method :
- By Using Unpacking
- Multiple Assignment
- Using F-Strings
Python Print Variable by using Unpacking
Unpacking is a process of assigning values from data structure to multiple variables in a one way. In this example, we have defined three variables ‘name’, ‘age’, and ‘city’ and assigned values to them simultaneously. Each variable receives its corresponding value from the right-hand side of the assignment.
name, age, city = "John", 30, "New York"
print(name)
print(age)
print(city)
Output:
John
30
New York
Python Print Variable by Multiple Assignment
You can assign multiple values to multiple variables using multiple assignment operators. In below code we assigned the values 1, 2, and 3 to variables a, b, and c, and then print the values using Multiple Assignment.
a,b,c = 1,2,3
print(a,b,c)
Output:
1 2 3
Python Print Variable using F-Strings
You can assign multiple values to multiple variables using F-Strings . Here, F-strings are utilized to insert variables (`name`, `age`, and `city`) into the string, allowing the creation of a customizable formatted message.
name = "Alice"
age = 30
city = "Wonderland"
# Using F-string to incorporate multiple variables
message = f"Hello, my name is {name}, I am {age} years old, and I live in {city}."
# Printing the formatted message
print(message)
Output :
Hello, my name is Alice, I am 30 years old, and I live in Wonderland.
Print Single and Multiple variable in Python
In this discussion, we will delve into the concept of variables and learn how to output both single and multiple variables in Python.