×

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

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand,...

3 minutes read.

Hog Descriptor Opencv Python

Python programming language: 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...

3 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

Python maketrans() function

Introduction A static method is the string maketrans() one. It is used to make a one-to-one mapping between a string's character and its translation, i.e., to define the list of characters...

5 minutes read.

NSE Tools In Python

About NSE NSE (National Stock Exchange) of India Limited is the advanced stock exchange of India. It is located in Mumbai, Maharastra and It was organized in 1992. It was...

2 minutes read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 minutes read.

Python String isupper() method

Python String isupper() method The String.isupper() method in Python returns a boolean value ‘true’ if all cased characters in the string are uppercase else for any other value is returns false. Syntax String.isupper() Parameter NA Return This...

2 minutes read.

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Python Image Processing

What is an Image? Images are the pictures that will define the world, and it has their own story, and consists of information about them and these are useful in many...

9 minutes read.

Python Dictionary

Dictionary in Python is an unordered collection of data values, which is used to store data values like a map. Unlike different data types that hold just individual assets as...

4 minutes read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Python program to check whether a given number is prime or not

Python program to check whether a given number is prime or not A positive integer greater than 1 is called a prime number if it is divisible by one and number...

1 minute read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

Python sklearn train_test_split

Sklearn: One of the most beneficial open-source libraries for learning algorithms in Python is, without a doubt, Sklearn or Scikit-Learn. The most effective tools for statistical modeling and machine learning are all included in...

6 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Difference between Python and CPP

Difference between Python and C++ We all know that both C++ and Python are two of the most popular programming languages. Both C++ and Python shares some basic similarities such as...

6 minutes read.

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 minutes read.