How do i print multiple variables in one line python?

Printing one variable is an easy task with the print[] function but printing multiple variables is not a complex task too. There are various ways to print the multiple variables.

To print multiple variables in Python, use the print[] function. The print[*objects] is a built-in Python function that takes the *objects as multiple arguments to print each argument separated by a space.

There are many ways to print multiple variables. A simple way is to use the print[] function.

band = 8
name = "Sarah Fier"

print["The band for", name, "is", band, "out of 10"]

Output

The band for Sarah Fier is 8 out of 10

In this code, we are printing the following two variables using the print[] function.

  1. band
  2. name

Inside the print[] function, we have put the variable name in their respective places, and when you run the program, it reads the values of the variables and print their values.

This is the clearest way to pass the values as parameters.

Using %-formatting

Let’s use the %-formatting and pass the variable as a tuple inside the print[] function.

band = 8
name = "Sarah Fier"

print["The band for %s is %s out of 10" % [name, band]]

Output

The band for Sarah Fier is 8 out of 10

Pass it as a dictionary

You can pass variables as a dictionary to the print[] function.

band = 8
name = "Sarah Fier"

print["The band for %[n]s is %[b]s out of 10" % {'n': name, 'b': band}]

Output

The band for Sarah Fier is 8 out of 10

Using new-style formatting

With Python 3.0, the format[] method has been introduced for handling complex string formatting more efficiently. Formatters work by putting in one or more replacement fields and placeholders defined by a pair of curly braces { } into a string and calling the string.format[].

It is a new style of string formatting using the format[] method. It is useful for reordering or printing the same one multiple times.

band = 8
name = "Sarah Fier"

print["The band for {} is {} out of 10".format[name, band]]

Output

The band for Sarah Fier is 8 out of 10

Python f-string

We can use the f-string to print multiple variables in Python 3. Python f String is an improvement over previous formatting methods.

It is also called “formatted string literals,” f-strings are string literals that have an f at the starting and curly braces containing the expressions that will be replaced with their values.

band = 8
name = "Sarah Fier"

print[f"The band for {name} is {band} out of 10"]

Output

The band for Sarah Fier is 8 out of 10

That’s it for this tutorial.

Related posts

How to Print Type of Variable in Python

How to Print an Array in Python

How to Print Bold Python Text

In the general case, you can give comma-separated values to print[] to print them all on one line:

entries = ["192.168.1.1", "supercomputer"]
print "Host:", entries[0], "H/W:", entries[1]

In your particular case, how about adding the relevant entries to a list and then printing that list at the end?

entries = []
...
entries.append[split[1]]
...
print entries

At this point you may want to join the 'entries' you've collected into a single string. If so, you can use the join[] method [as suggested by abarnert]:

print ' '.join[entries]

Or, if you want to get fancier, you could use a dictionary of "string": "list" and append to those lists, depending on they key string [eg. 'host', 'hardware', etc...]

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In Python 3.X, the print statement is written asa print[] function. Below is code in Python 3.X that shows the process of printing values in Python.

    Example 1: Printing Single value

    Python3

    Output: 

    1 
    1

    Example 2: Printing multiple values  

    Python3

    print[1, 2]

    print[[1, 2]]

    print[1, end=" "]

    print[2]

    Output: 

    1 2
    [1, 2]
    1 2

    This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 

    How do I print multiple variables on one line?

    You can use the cat[] function to easily print multiple variables on the same line in R. This function uses the following basic syntax: cat[variable1, variable2, variable3, ...] The following examples show how to use this syntax in different scenarios.

    Can you print multiple variables in Python?

    To easily display single and multiple variables in Python, use the print[] statement. For multiple variables, use the comma operators.

    How do you print multiple lines with one print in Python?

    “how to print multiple lines using one print statement in python” Code Answer's.
    #You can use \n to create a carriage return..
    print["first line\nSecond line"].
    #Or use the following syntax to replace the commas with \n..
    #to create carriage returns for each line..
    print["first line", "second line", sep="\n"].

    How do I print multiple strings and variables in Python?

    In Python, we can print multiple variables easily with the print[] function. Just pass each variable to print[] separated by commas to print multiple variables on one line. In Python, when writing programs, being able to check the value of certain variables can be useful.

    Chủ Đề