Open In App

Print Single and Multiple variable in Python

Improve
Improve
Like Article
Like
Save
Share
Report

In this discussion, we will delve into the concept of variables and learn how to output both single and multiple variables in Python.

What is a Variable?

Variables are containers in Python that store values. In Python, You can work with both single and multiple variables. By understanding the difference of variables you can write an optimized code. In this article, you can find How we can print single and multiple variables in Python.

In Python, the print statement is written as a print() function. Below is code in Python 3 that shows the process of printing values in Python.

Example:– Printing a variable in Python is easy. You just use the print() function. Here’s an example:

GeeksforGeeks = "Hello, Geek!"
print(GeeksforGeeks)

Here, “GeeksforGeeks” is the variable, and we’ve put the words “Hello, Geek!” in it. The print() function shows what’s inside the variable on the screen.

Single Variable in Python

A single variable in Python is a container that can store a single value or data item. These values can be of various types, such as integers, floating-point numbers, strings, or other data types. Defining a single variable is as simple as assigning a value to a variable name using the equal sign (‘=’).here we are discussing some generally used methods which are as follows:

  • Using Print Function
  • Using concatenation
  • Using F-Strings

Print Variable in Python using Print() Function

Print Function is a method to print single variable easily just write variable in print() .In this example, we’ve created a single variable named ‘x’ and assigned it the value 42. You can use ‘x’ in your code to access or manipulate this value.

Python3
x=42 #Assigning an integer value to the variable "x"
print(x)

Output:

42

Print Variable in Python using Concatenation

Concatenation is a method to print single variable easily just concatenate variable in print() function .In this example, we’ve created a single variable named name and assigned it the value GeeksforGeeks. You can use name in your code to access or manipulate this value.

Python3
name = "GeeksforGeeks"
print("Name:", name)

Output :

Name: GeeksforGeeks

Print Variable in Python using F-Strings

F-String is a Method print single variable easily just use F-string for print single variable. In this example , code sets the variable `name` to “Alice” and prints it using an f-string, resulting in the output: `Name: Alice`.

Python3
name = "Alice"
print(f"Name: {name}")

Output :

Name: Alice

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.

Python3
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.

Python3
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.

Python3
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.


Last Updated : 14 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads