×

How to print in same line in python

How to Print in the Same Line in Python

To print in Python, we use the print () function and the syntax of print () goes like this:

print (values, sep = '', end = '\n', file = file, flush = flush)

Here,

  • end : It is an optional parameter. It is used to specify the character to be printed at the end. Default value is a new line character – end = ‘\n’.

By default, in Python, when we write a print function and it is executed immediately, the cursor moves to the next line. If we write 2 print () functions, they will be printed in two different lines.

In other programming languages like C and C++, it is not like this; It means, if we write multiple printf () functions they will all be continued in the same line. We need to use \n to switch to the next line.

In this article, we will discuss a few ways in Python to stop printing and transferring cursor to the next line and print continuously in the same line.

First, let us see how normally print () function executes?

print ("Hello")
print ("World")

Output:

Hello
World

Now, to get Hello World in the output:

1. Using the end parameter in print ():

As we discussed at the top of the article,

The end parameter specifies the character to be printed at the end. Default value is a new line character – end = '\n'.

We can assign an empty character or a space to the end parameter.

print ("Hello", end = ' ') 
print ("World")

Output:

Hello World

2. Using a comma(,) : (Python 2. x)

Program:
print("Hello"),
print("World")
 
arr = [1, 2, 3, 4]
 
for i in range(4):
    print (arr [i]),

Output:

Hello World
1 2 3 4

Understanding:

This only works in the old version – Python 2. x in Python. Using a comma after the print () statement can avoid going to the next line to print the next statement.

3. End parameter while looping

Program:

list1 = [1, 2, 3, 4]
for i in list1:
    print (i, end = " ")

Output:

[1, 2, 3, 4]

Understanding:

We kept the print statement in a for loop. We assigned the end parameter with a space. For all iterations of i, the print is executed. i is printed, and the end is updated to space, thus not transferring to a new line but taking a space.

4. Using the *:

list1 = [1, 2, 3, 4]
print (*list1)
string1 = "Hello world"
print (*string1)

Output:

1 2 3 4
H e l l o  w o r l d

Understanding:

Using the * before printing the string or a list, we can print the elements or the characters of a list or a string separated by a space rather than using loops.

5. Concatenation ( only for sequence type data types)

Program:

a = "Hello"
b = "world"
print (a + " " + b)
c = [1, 2, 3]
d = [4, 5]
print (c + d)

Output:

Hello world
[1, 2, 3, 4, 5]

Understanding:

We have a concatenation method for sequence type data types like strings and lists, and + is called the concatenation operator. We can add or concatenate two separate elements ( strings or lists) using this method.

Example program with all ways:

string1 = input ("Enter a string: ")
string2 = input ("Enter another string: ")
print (string1 + " " + string2)
print (string1 + string2)
print (string1, end = " ")
print (string2)
print (* (string1 + string2))

Output:

Enter a string: Hello
Enter another string: World
Hello World
HelloWorld
Hello World
H e l l o W o r l d

Related Topics

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements. It can contain any kind of data type inside it but in order to concatenate two...

3 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Python Deep Copy and Shallow Copy

Assignment statements in Python create bindings between a target and an object, not copies of them. The = operator only makes a new variable that shares the reference to the...

3 minutes read.

Python md5_file() function

Python md5_file() function The md5_file() function in PHP calculates the md5 hash of a given file. Syntax md5_file ( string $filename [, bool $raw_output ] )  Parameter filename(required)- This parameter signifies the file to be calculated. raw_output(optional)- It takes a boolean value that specifies hex or binary...

1 minute read.

Python Break Statement

In Python, loops are used to automate and repeat processes in an effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration,...

2 minutes read.

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

Python RegEx

Python RegEx (python regular expressions) is a concept of writing and finding expressions in a pool of characters easily. A Regular expression (RegEx) is a set of characters that defines search...

10 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Python List reverse() method

 Python List reverse() method The list.reverse () method in Python reverses the elements of the list in place. Syntax list.reverse() Parameter NA Example 1 # Python program explaining # the list.reverse() method weekList = ['Sun','Thurs','Mon','Fri', 'Tues', 'Wed'] print("Actual week list: ",weekList) #...

1 minute read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

Python Os sep

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Operator Precedence

Before knowing about the operator precedence in Python, we have to know about the operators in Python. So let's have a look at it. According to one definition, the operator is...

3 minutes read.

Read JSON File in Python

JSON refers to the JavaScript Object Notation. It is a Data format used for representing structured data and it is used to transfer and store data. In JSON format, the...

5 minutes read.

Python Program for Tower of Hanoi

In this tutorial, we will examine and learn about the Python coding used to create the video game Tower of Hanoi. Before seeing the game's step-by-step implementation in Python, we...

3 minutes read.

Division in Python

In this tutorial, we will understand what mathematical operation division is and how is it performed in Python Programming language. We will also focus on the operators being used in...

3 minutes read.

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.