How to Print a String Without ‘\n’ in Python

Strings, Printing and ‘\n’

The printing of a basic string is probably the first program the majority of people, myself included, write in Python –  does print('hello world') sound familiar? From the outset we know the principle is simple, we can pass our string through the print() function and the output will be displayed in either the command-line or interactive interpreter.   

As we work more with strings, we get to know special characters which help us with our formatting – of which  '\n' is amongst the most common. In Python strings,  '\n' is used to denote a newline,  i.e., the end of a line and the beginning of a new one. There are clearly times when we specifically want to include newlines in our strings, to make the text more readable for example, however, there are also times when we do not want the newline to be printed.  

In this article, we will look at how to print a string without including the newline. As this will differ depending on the version of Python being used we will focus on Python v3 only.

Method 1: Newlines and the Python print() function

By default, the built-in Python print() function adds the newline character to the end of every print statement. This is done on the basis that if we are using multiple print statements each statement requires a separate output, so each output should appear on a newline. For example: 

print('Hello, this is the first line')
print('and this is the next one')

As you can see, we have not included any newline ('\n') characters, these are clearly two independent print statements. From a purely logical point of view the output printed as two separate lines should not be a surprise: 

Hello, this is the first line
and this is the next one

However, suppose there is a reason we want the output to be printed on the same line but keep separate print statements? To do this we need to look at the syntax behind the print function, which contains the following parameters: 

print(value,……., sep = ' ', end = '\n', file = sys.stdout, flush = False)

The parameter we are particularly interested in is  end = '\n',  which is telling Python that every time we call this function it needs to end with a newline. So, even though we aren’t explicitly requesting a newline, it is being included by default. To stop this from happening we just have to change the default value of the 'end' parameter from '\n' to whatever we want it to be. If we don’t want there to be anything printed we can leave it blank: 

print('Hello, this is the first line' end='')
print('and this is the next one')

As a newline is no longer the default value in the above code, Python gives the following output: 

Hello, this is the first lineand this is the next one

We can even format to include a space in-between the two lines, by adding a space to the 'end' parameter as follows: 

print('Hello, this is the first line' end=' ')
print('and this is the next one')

This time our output is properly formatted: 

Hello, this is the first line and this is the next one

Method 2: The sys.stdout Alternative

Another option would be to avoid using the print() function to display our string altogether. As an alternative we can directly invoke the sys.stdout.write() method. This has the advantage of not automatically including a newline every time we pass a string through it. 

If we refer back to the previous section and the syntax of print() function, we can even see that sys.stdout is actually one of the parameters: ‘file = sys.stdout’. stdout stands for standard output pipe which is where the string is automatically sent, i.e our command-line or interactive interpreter. 

It is therefore possible to pass a string directly through  sys.stdout.write(), effectively bypassing the print() function. Please bear in mind though, whilst we do not have to download the sys module, it does need to be imported before we can use it: 

import sys
sys.stdout.write('Hello, this is the first line')
sys.stdout.write('and this is the next one')

Output: 

Hello, this is the first lineand this is the next one

In this example our output has been ‘written’ to the console directly. As we have not used the print() function there is no issue with the newline default value of the 'end' parameter. On the downside, it is not possible to format the display in the same way as we can using print()

Method 3: Newlines Within the String

So far we have looked at how to stop the newline ('\n') being automatically added when we want to print our string. Now, let’s see what happens when the newline characters are actually contained within our string but we do not want them to be printed. 

Let’s first create a string containing a newline break,  and assign it to the variable 'a'.  

a = 'Hello, this is the first line\nand this is the next one'
print(a)

Now, if we print the variable the output will display across two lines as we have included the special characters '\n' within the script, which Python knows to execute as a newline.

Output:

Hello, this is the first line
and this is the next one

In this case,  because the '\n' characters are physically contained within our string we can just use the replace() string formatting method. This returns a copy of the string where all instances of a certain substring are replaced with another substring.  We can choose to replace the substring, and only that substring, with anything we want – including an empty character:   

a=a.replace('\n', '')
print(a)

Output: 

Hello, this is the first lineand this is the next one

In addition the replace() method has a 'count' parameter that allows us to specify how many times we want to replace the old substring with the new one. If no value is specified it will simply replace all instances of the old substring. To illustrate, let’s update our example to include multiple newline characters:   

b = 'Hello, this is the first line\nand this is the next one\nand this is the next one\nbut I want this on a different line please'
print(b)

Output: 

Hello, this is the first line
and this is the next one
and this is the next one
but I want this on a different line please

In this example, I do not want the first two newline characters but I want to keep the third in-place. In addition, rather than just an empty character I want to replace the '\n' with a space, for formatting reasons only:

b=b.replace('\n', ' ', 2)
print(b)

Output: 

Hello, this is the first line and this is the next one and this is the next one
but I want this on a different line please

Summary

In this article we have looked at how to print a string without the newline ('\n'). Whilst there are various reasons this may be necessary, how we can stop it happening primarily depends on whether the newline is part of our string or is being invoked automatically through the print() function. 

If it is being invoked with the print() function, we have seen how changing the default of the end parameter can overcome this. This, in my opinion, is the most straightforward option. Whilst the alternative sys.stdout function also provides a solution it seems a very complicated way of achieving the same result. 

When the newline is actually part of the string the replace() function allows us to specify the substring we want replaced. It gives us the flexibility of not only specifying what we want to replace it with, but also stipulate how many times we want it replaced.