Python

How to print without newline in Python

Hello in this tutorial, we will see how to print without a newline in python programming.

1. Introduction

Printing or logging is the most common thing in any programming language and it is one of the most common things that everyone enjoys doing. By default, every print output is written in a different line in python programming. To print without a new line we simply have to add an argument to the print statement or use an inbuilt python library (i.e. sys).

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. How to print without newline in Python

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice.

2.1 How to print without newline in Python

Let us understand this with the help of a code snippet.

Single inheritance

# print without newline in python 3.x
# python inbuilt library to print without newline
import sys
def simple():
    print('Hello!', end=' It is friday today. ')
    print('Have a great weekend.')
def loop():
    cars = ['Ford', 'Volvo', 'Audi', 'Mercedes', 'Toyota']
    for car in cars:
        print(car, end=' ')
def using_sys():
    sys.stdout.write('Hello! It is friday today. ')
    sys.stdout.write('Have a great weekend.')
def main():
    simple()
    print('\n==========\n')
    loop()
    print('\n==========\n')
    using_sys()
if __name__ == '__main__':
    main()

If everything goes well the following output will be shown in the IDE console.

Console Output

Hello! It is friday today. Have a great weekend.
==========
Ford Volvo Audi Mercedes Toyota 
==========
Hello! It is friday today. Have a great weekend.

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • How to print without a new line in python programming
  • Sample program to understand the implementation

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial on how to print without a new line in python programming.

Download
You can download the full source code of this example here: How to print without newline in Python

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button