Python Print without a Newline

How to print without a newline (or line break) in Python? To display a string without a newline either use the comma separator when using Python 2.x or use the end parameter when using Python 3.0 version.

Python by default prints every string with a new line to the console, hence every print statement you use, displays the string in a new line. However, Python provides different ways to use a print() to display each statement as an append to the previous statement resulting in a single line (without writing to a newline).

Methos to print without a newline in Python :

  1. Use the end parameter on the print() function. (useful to add any character between text)
  2. Write a print statement by ending with a comma (adds space between text)
  3. By using the sys module (no space added between text)

1. Quick Examples of Printing without Newline

Following are quick examples of printing without a newline


# Printing text without newline (Python 3.x)
print("Welcome to", end ="")
print("Python Tutorial")

# Printing text without newline
# Add space between each text
print("Welcome to", end =" ")
print("Python Tutorial")

# Printing text without newline (Python 2.x)
print "Welcome to",
print "Python Tutorial"

#Import sys module
import sys
sys.stdout.write("Welcome to")
sys.stdout.write("Python Tutorial")

2. Default Behaviour of Printing

By default when you display strings in Python by using the print() function, each string issued with a print function writes in a new line. Here, is an example.


print("Welcome to")
print("Python Tutorial")

This code yields the following output on the console.

python print without newline

3. Print Without a New Line in Python 3.x

In Python 3.x, to display without a newline use end param to the print() function. This end parameter tells the python runtime to display the string with the next print statement in the same line. With this you can understand that print() statement by default takes "\n" value to the end param hence, each print results in a new line.


# Printing text without newline (Python 3.x)
print("Welcome to", end ="")
print("Python Tutorial")

This code yields the following output on the console.


# Output:
Welcome toPython Tutorial

Note that in the above example, both strings are displayed in the same line but without any space between each string, this is because I used an empty string in our quote marks, which means that no spaces were added between our text on the new line.

Now let’s use the space value with the end param.


# Printing text without newline
# Add space between each print
print("Welcome to", end =" ")
print("Python Tutorial")

This code yields the following output on the console.


# Output:
Welcome to Python Tutorial

Similarly, you can print strings in the same line with any separator between each string. For example, to display comma separator between each print you can use end=","

4. Print Without a New Line in Python 2.x

The end param is not available to use in Python 2.x hence, you need to use a comma at the end of your print statement. Here’s an example. Note that is approach will not work with Python 3.x


# Printing text without newline (Python 2.x)
# Add space between each print
print "Welcome to",
print "Python Tutorial"

This code yields the following output on console.


# Output:
Welcome to Python Tutorial

Note that this approach by default adds the spach character between each string when it displays to console.

5. Print without Newline using Python sys Module

Some times you would be required to print a text without a newline in Python 2.x, the above approach using comma at the end of the print statement might not be sufficient as it adds a space between each statement.

To overcome this you can use the stdout.write() from the sys module to print the text as-is to console without adding a newline or a space character. In order to use this, first you need to import the sys module.


# Printing without newline
# Using sus module

#Import sys module
import sys

sys.stdout.write("Welcome to")
sys.stdout.write("Python Tutorial")

This code yields the following output on console.


# Output:
Welcome toPython Tutorial

Here, the import sys statement imports the sys module so that we can use the methods from this package. sys.stdout refers to standard output which is a console by default, if you are using logging, it may refer to log file as-well. sys.stdout.write() statement actually writes the text to the standard output as-is with out any newline or space. And when we issues second write statement it just appends to the existing line.

6. Print List of Strings in a Newline using Loop

Finally, let’s see how to print the list of strings in a single line while using for loop. Here, I just use the examples explained above.


mystring = ["apple","mango","grape","berry"]
for x in mystring:
    print(x, end = " ")

This code yields the following output on console.


# Output:
apple mango grape berry 

Conclusion

In this article, you have learned by default each print statement display the strings in a new line and learned different ways to print each statement without a new line in Python.

Related Articles

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply