To print on the same line in Python, add a second argument, end=’ ‘, to the printer function. This will keep the text from breaking to a new line.
One way to print on the same line in Python is by using the print command with an end argument. The end argument allows you to specify what character should be used at the end of a line when printing.
Example:
print("Hello", end="")
print(" World!")
Output:
Hello World!
This will result in the text “Hello World!” is printed on the same line. I want to let you know that you should not include a space between the two print commands, as this will cause an area to be printed between the words.
Another way to print in Python on one line is using the write() method instead of print(). This method allows you to write any data directly to your standard output without adding any newline characters or other whitespace.
Example:
import sys
sys.stdout.write("Hello")
sys.stdout.write(" World!")
Output:
Hello World!
This will result in the same output as before, “Hello World!” with no extra whitespace. The write() method does not automatically add a newline new liner after each call, so you will need to specify it manually if you want to.
Finally, there are also libraries available for Python explicitly designed for printing on one line or other specific formatting needs. These libraries may offer more convenience and control than either of the two methods mentioned above and are worth exploring if you need more advanced text formatting capabilities.
In conclusion, there are several ways to print on the same line using Python depending on your specific needs: you can use the
end =”” statement, the write() method, or a library. Whichever option you choose, it is now easy to print without adding extra blank lines in your output.
Printing in Python
If you are writing a Python program, there will be times when you want to print stuff on the same line.
This is useful for debugging your code or testing how it generally works. For example, when you have consecutive print() function calls, Python automatically adds a new line; however, sometimes, this default behavior is not what you want.
In this guide, you will learn how to print on the same line in a couple of easy ways. Let’s briefly discuss strings before getting there.
If you are new to programming altogether, review strings again at this time.
What Is a Python String
Python strings are sequences of characters that can be used to represent objects, text, numbers, or other data. They are similar to strings in different programming languages but are much more powerful and flexible. A Python string can contain any combination of characters, including spaces, punctuation marks, symbols, and numbers. They are also immutable, meaning that once a string is created, it cannot be changed.
You can use either:
- Single quotes.
- Double quotes.
Example:
s1 = "This is a Text1"
s2 = 'This is a Text2'
# 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".'
Print without New Line in Python
In Python, the “end” argument of the print() function is used to specify what needs to be printed at the end of the line.
By default, it is “\n” or the newline character, meaning that the following print statement will be printed on the following line.
s1 = "This is a Text1"
s2 = 'This is Text2'
print(s1)
print(s2)
Output:
This is a Text1
This is a Text2
When strings are printed this way, it introduces a new line. Although this is convenient, it’s only sometimes what you want.
If your instead Python didn’t add new lines when printing, you can specify another parameter inside the print() function.
The optional “end” parameter indicates the last character to be printed after the string.
If no input is given, the default is a newline character, demonstrated by the trailing carriage return after every print() function call.
However, this can be changed to any desired value. For example, rather than introducing a new line,
replace end with a space” “.
Example:
s1 = "This is a Text1"
s2 = "This is a Text2"
print(s1, end=" ")
print(s2)
Output:
This is a Text1, This is a Text2
For example, let’s set the end parameter such that a print() function call would end with 3 line breaks:
s1 = "This is a Text1"
s2 = "This is a Text2"
print(s1, end="\n\n\n")
print(s2)
Output:
This is a Text1
This is a Text2
Now you know how to print strings side by side in Python.
To be more concise, you now comprehend how to shift the default end figure of a print() function call.
Alternative Ways to Print on Same Line in Python
Printing on the same line in Python can be done using various techniques. The most straightforward approach is to use the print() function with the end parameter set to an empty string. This will tell the print function not to add a new line at the end of the output.
When using the print() function, no new lines will be inserted if you include multiple items.
Example:
s1 = "This is a Text1"
s2 = "This is a Text2"
print(s1, s2)
Output:
This is a Text1 This is a Text2
Adding a new line would create extra space, so instead a blank space is added. This makes sense when printing multiple items at once.
Join the Strings Before Printing
The join() method in string allows for the joining of two or more strings with a specific character. This is useful for printing multiple strings on the same line. The join() method must be invoked with a delimiter (separator) string as its argument.
separator.join(elements)
The separator variable is a string between elements in the list of aspects. For example, if you wanted to join two strings with a space in between, you would use the join() method.
Example:
s1 = "This is a Text1"
s2 = "This is a Text2"
combined = " ".join([s1, s2])
print(combined)
Output:
This is a Text1 This is a Text2
Although it may not seem like it, this following method technically prints on the same line. Instead of two separate strings, we combine them into one string before printing it out. In addition, you now know multiple ways to print strings on the same line in Python. For our last example, let’s explore how things work in older versions of Python 2
How to Print on the Same Line in Python 2.xx
Unlike in Python 3, print is not a function in Python 2 – it’s a statement. So, to get your print statement on the same line as something else when using Python 2.x, add a comma after your print statement.
Example:
s1 = "This is a Text1"
s2 = "This is a Text2"
print (s1)
print (s2)
This is a Text1 This is a Text2
Wrap up
Today, you learned how to print on the same line in Python. To recap: unless specified otherwise, the print() function automatically adds a newline character at the end of each string it prints.
Therefore, to prevent Python from printing a new line, you can use one of these three options:
- Specify an optional parameter end as a blank space.
- Call the print() function with multiple inputs.
- Use the string’s join() method to combine the strings before printing.
Thanks for reading. Happy coding!