How do you print multiple variables on the same line in python?

How do you print multiple variables on the same line in 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.

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

To print on the same line in Python, add a second argument, end=’ ‘, to the print() function call.

For instance:

print("Hello world!", end = ' ')
print("It's me.")

Output:

Hello world! It's me.

This is the quick answer.

However, if you want to understand how this works, I recommend reading further.

Printing in Python

When writing a Python program, you commonly want to print stuff on the screen.

This is useful if you are debugging your program or testing how it works in general.

When you introduce consecutive print() function calls, Python automatically adds a new line.

However, sometimes this is not what you want.

In this guide, you will learn how to print on the same line in Python in a couple of easy ways.

Let’s briefly discuss strings before getting there. If you are a beginner, it is worthwhile to recap strings.

What Is a Python String

A Python string (str) is a common data type used to represent text.

To create a string, wrap letters or numbers around a set of quotes.

You can use either:

  1. Single quotes.
  2. Double quotes.

Here are some examples of strings:

s1 = "This is a test"
s2 = 'This is another test'

# To add quotes in a string, use double quotation marks with single-quote strings and vice versa.

q1 = "Then he said 'Bye'."
q2 = 'She told us "It is time to celebrate".'

To display strings in Python, you can print them into the console using the built-in print() function.

For example:

s1 = "This is a test"
s2 = 'This is another test'

print(s1)
print(s2)

Output:

This is a test
This is another test

Printing the strings this way introduces a new line.

Meanwhile, this is intuitive, it is not always what you want.

To make Python stop adding new lines when printing, you can specify another parameter inside the print() function.

This parameter is an optional parameter called end. It specifies the last character after printing the string.

By default, the end parameter is a newline character, that is, “\n”.

This is why there is always a new line after a print() function call.

But you can change the end parameter to whatever you want.

To not want to introduce a new line, replace the end with an empty space ” “.

For example:

s1 = "This is a test"
s2 = "This is another test"

print(s1, end=" ")
print(s2)

This results in both strings being printed on the same line:

This is a test This is another test

As another example, let’s set the end parameter such that a print() function call ends with 3 line breaks:

s1 = "This is a test"
s2 = "This is another test"

print(s1, end="\n\n\n")
print(s2)

This results in 3 new lines being printed in-between the two prints:

This is a test


This is another test

Now you know how to print strings on the same line in Python.

To be more formal, you now understand how to change the default end character of a print() function call.

Alternative Ways to Print on Same Line in Python

Let’s finally go through some alternative approaches to printing strings on the same line in Python.

Pass Multiple Strings into the Same print() Function Call

Python print() function accepts any number of positional arguments.

This means you can give it as many arguments as you want, and it will print them all into the console.

If you print multiple elements in the same print() function call, there will be no new lines added.

For instance:

s1 = "This is a test"
s2 = "This is another test"

print(s1, s2)

Output:

This is a test This is another test

Instead of adding a new line, a blank space is added. This makes printing multiple items in a single call behave in an intuitive way.

Join the Strings Before Printing

Another way to print strings on the same line in Python is by utilizing the string join() method.

The join() method follows this syntax:

separator.join(elements)

Where separator is a string that acts as a separator between each element in the elements list.

For example, you can join two strings by a blank space to produce one long string and print it.

Here is how it looks in code:

s1 = "This is a test"
s2 = "This is another test"

combined = " ".join([s1, s2])

print(combined)

Output:

This is a test This is another test

However, technically this is not printing on the same line. Instead, we combine two strings into one string and print it as a single string.

Now you know a bunch of ways to print strings on the same line in Python.

Last but not least, let’s see how things work in older versions of Python 2.

In Python 2 print is not a function but a statement.

To print on the same line using Python 2.x, add a comma after the print statement.

For example:

s1 = "This is a test"
s2 = "This is another test"

print s1,
print s2

Output:

This is a test This is another test

Conclusion

Today you learned how to print on the same line in Python.

To recap, the print() function adds a newline character to the end by default. This means each print() call prints into a new line.

To not print a new line, you can choose one of these three options:

  1. Specify an optional parameter end as a blank space.
  2. Call the print() function with multiple inputs.
  3. Use string’s join() method to combine the strings before printing.

Thanks for reading.

Happy coding!

Further Reading

All String Methods in Python

50 Python Interview Questions and Answers

How do I print multiple variables in one line in Python?

To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.").
Specify an optional parameter end as a blank space..
Call the print() function with multiple inputs..
Use string's join() method to combine the strings before printing..

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.

How do you print 3 variables in Python?

You can see, three string variables are displayed by using print function. Each variable is separated by a comma. In the output, space is added automatically. This is due to the parameter sep = '', as shown in the syntax of the print function.

Can you set multiple variables in one line Python?

Python assigns values from right to left. When assigning multiple variables in a single line, different variable names are provided to the left of the assignment operator separated by a comma. The same goes for their respective values except they should be to the right of the assignment operator.