All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Print On Same Line Without Space

Last Updated : Jul 1, 2023

Python Print On Same Line Without Space

In this tutorial we will show you the solution of python print on same line without space, when working as a programmer, you commonly want to print something as the output of the program.

You can use printing for displaying output or you can use it for debugging in a program. So let’s get started.

Step By Step Guide On Python Print On Same Line Without Space :-

Python provides a function which you can use to print something as the output.

Python provides the print() function, which you can use in your program to display something to the user.

Whenever you use the print() function consecutively to display something in the output, the print() function will add a newline at the end of the statement.

Sometimes there might be a need to display two or more statements on the same line, but Python’s print() function doesn’t support that by default.

#Using the end parameter to print on same line without space
string1 = "Hello World!"
string2 = "How are you?"
print(string1, end=" ")
print(string2)
string3 = "Hello World!"
string4 = "How are you?"
print(string3, end="\n\n\n")
print(string4)

#Pass multiple strings in print() function
string5 = "Hello World!"
string6 = "How are you?"
print(string5, string6)

#Using join() function to print on same line without space
string7 = "Hello World!"
string8 = "How are you?"
print(" ".join([string7, string8]))
  1. The first line is a comment saying “Using the end parameter to print on same line without space”, which is the first method you can use to print on the same line without space.
  2. The next 2 lines contain 2 different variables with some string values assigned to them.
  3. The next line is a print statement which has a parameter of end attached to it. By default, the value of the end parameter is newline, which is why there is a newline after each print statement in the output. You can always specify your own value for the end parameter to modify the output as you want.
  4. Here, in the first attempt to use the end parameter, you can provide the value of “ ”(white space), which will not include any newline at the end of the print statement.
  5. In the next line, there is a print statement without the end parameter attached, which will be displayed in the same line as the above print statement.
  6. While in the second attempt here, we have assigned a value of “\n\n\n” which will display the next print statement’s value after 3 blank lines in the output.
  7. The next line again contains another comment saying “Pass multiple strings in print() function”, which you can use to print on the same line without space.
  8. The next 2 lines each contain 2 variables with some demo string value assigned to them.
  9. The next line is a print statement that has 2 string variables as the arguments. These 2 strings will be displayed on the same line because they are passed to a single print statement.
  10. The line is a comment as well, which says, “Using join() function to print on same line without space”, which you can use to print on the same line without space.
  11. The next two lines contain two variables, each with a demo string value assigned to it.
  12. With the use of the join() function, you can join two strings by a blank space to produce one long string and print it.

Conclusion :-

So finally, in conclusion, we can say that with the help of this tutorial, you can now print on the same line in a Python program.

I hope this tutorial on python print on same line without space helps you and the steps and method mentioned above are easy to follow and implement.