- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to avoid printing newline in Python?
By default Python adds a new line when a text is printed using the print() method. We will see here how we can avoid the following −
Example
print("Demo") print("This is another demo text.")
The output prints them in a new line −
Output
Demo This is another demo text.
To avoid what we have shown above, use the end parameter.
Avoid printing newline - strings
In this example, we will see how we can avoid displaying newline while working around strings −
Example
# Displaying in new lines print("We cannot") print("displaying this") print("in a single") print("line") print("") # Displaying in a single line i.e. avoiding new lines print("We are ",end =""), print("displaying this ",end =""), print("in a single ",end =""), print("line")
Output
We cannot displaying this in a single line We are displaying this in a single line
Avoid printing newline - numbers
In this example, we will see how we can avoid displaying newline while working around numbers −
Example
# Displaying in new lines # Defining a list my_arr = [5, 10, 15, 20, 25, 30] # printing the list content for i in range(4): print(my_arr[i]), print("") # Displaying in a single line i.e. avoiding new lines # Defining a list my_arr = [5, 10, 15, 20, 25, 30] # printing the list content for i in range(4): print(my_arr[i], end =""),
Output
5 10 15 20 5101520
- Related Articles
- How to print without newline in Python?
- How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script?
- Printing to the Screen in Python
- How to split on successions of newline characters using Python regular expression?
- Multi-Line printing in Python
- How to match tab and newline but not space using Python regular expression?
- Putting a newline in Matplotlib label with TeX in Python
- Programs for printing pyramid patterns in Python
- Avoiding quotes while printing strings in Python
- How to Remove Newline Characters from a text File?
- How to increase the printing limit in R?
- How to avoid “StaleElementReferenceException” in Selenium?
- Why do backslashes appear twice while printing in Python?
- How to avoid bugs in cloud computing
- How to Avoid Errors in Java Code?

Advertisements