How to Print in Same Line in Python?
Overview
Printing in the same line in Python can be achieved in several ways, including using the "end" parameter in the print() function to set it to an empty string, the sys.stdout.write() function to write to standard output without a newline character, or the "sep" parameter in the print() function to concatenate strings and print them without a separator. The most common way in Python 3 is to use the "end" parameter. Choose the method that best suits your specific use case and Python version.
Print without Newline in Python 2.x
In this section, we will be looking at how to print in the same line in the Python2.x version. Lets us understand this section using code.
Code:
Output:
The above code is written in Python2.x, in the code, we just print the strings using print statements, and at the end of each print statement, , is used to print all the print lines in the same line.
Note:
As mentioned the above code is written in Python2.x, but as per today's date(17 July 2022), we are working in Python3.x version, so if we write the Python2.x code in Python3.x, so it might not work. So, before writing the above code in Python, make sure that one should first installs Python2.x installer, and then run the above code.
Print without Newline in Python 3.x
Switching from Python2.x to Python3.x, we saw some minor changes appear in our print statement and Python3.x it becomes a more user-friendly and easy way to write syntax.
As it is predefined in the print statement, for every print statement it will automatically switch to a new line. But in this case, we must understand how to print in the same line.
So, for printing in the same line, we need to add a parameter in the print statement, that will help us to print in the same line.
Syntax:
Parameter:
- variable:
Enter the variable that you want to print. - end:
It is an optional parameter. It specifies the last character after printing the string. By default, the end character is end = "\n", i.e., a new line character. So, by default, it will always go to a new line after calling the print() function.
As mentioned, it is an optional parameter, so we can change it like if end = "-", then the last character will be -.
To not want to introduce a new line, replace the end with a space ” “.
Return:
- It will print the content that is stored in the variable.
Code:
Output:
Print without Newline in Python 3. x without Using "for" Loop
In this section, we will see how to print the content in a single line without using a for a loop. As far as we know that in a for loop, the print command runs for n times, then every time it will print the content in a new line, but if we use end = " ", then we print the content in a single line, but also we are using for loop in that case.
Now, without using for loop and end = " ", we will see how to print the content in the same line.
Code:
Output:
Note:
* is a positional argument that helps print your data in a raw format. We can use other methods to remove commands and brackets from the array, but using * is simpler and can solve the problem of unpacking them.
Print on the Same Line with Space Between Each Element
Code:
Output:
In the above, we have printed the strings in a single with a space between every string.
We know that in the print() statement, we have a parameter end, which specifies the last character after printing the variable content. So, if we pass the space " ", in the end, parameter, it will separate our string value with a space after every string.
Print on the Same Line without Space Between Elements
Code:
Output:
In the above, we have printed the strings in a single one with no space between every string.
We know that in the print() statement, we have a parameter end, which specifies the last character after printing the variable content. So, if we pass the empty string "", in the end, parameter, it will separate our string value with an empty character after every string.
Print on the Same Line with Some Sign Between Elements
Code:
Output:
In the above, we have printed the strings in a single one with no space between every string.
We know that in the print() statement, we have a parameter end, which specifies the last character after printing the variable content. So, if we pass the hypen sign "-", in the end, parameter, then it will separate our string value with a hyphen after every string.
Python print() Function by Default Ends with a Newline
As far now we have seen different cases of using print statements, and we have seen the end parameter also. end parameter explains the last character after printing the variable content.
By default, the end parameter has character\n which represents the new line character.
Code:
Output:
Learn More
Get Certified in Python
Elevate your coding career with our Python Certification Test – your gateway to proving your expertise in Python and boost your resume!
- Practical and use case based questions
- Detailed Analysis of your performance
- Personalized video recommendations tailored to address any gaps in your knowledge.
Get your Python certification now
Conclusion
- To print in the same line, use the "end" parameter in the print() function and set it to an empty string.
- Use the sys.stdout.write() function to write to standard output without a newline character.
- Use the "sep" parameter in the print() function to concatenate strings and print them without a separator.