×

How to print in the same line in Python?

How to print in the same line in python

By default, the print function in Python takes us to the next line and prints the desired statement in the output.

In this article, we will discuss how we can print our statements in the same line in Python. We will try to understand the same with the help of some examples.

The following program illustrates how we use the print function in general. So, what we have done here is, we have stored two strings in two variables.

Later we used the print function to check the output.

 a="Tutorials"
 b="Tutorials and Examples"
 print("The value of a is {}".format(a))
 print("The value of b is {}".format(b)) 

INPUT-

print in the same line in Python

OUTPUT-

print in the same line in Python

In the above output, we have observed that the values of the variables a and b are printed in two separate lines.

In the next example, we will see how the 'end' keyword can help us to print both statements in a single line.

The following program illustrates the same-

 a="Tutorials"
 b="Tutorials and Examples"
 print("The value of a is {}".format(a),end=" ") #to print in one line
 print("The value of b is {}".format(b)) 

INPUT-

print in the same line in Python

OUTPUT-

print in the same line in Python

In the above output, we have observed that the values of both variables are printed in the same line.

The next example is based on printing the elements of an array.

In the given program, we have declared an array, and then used ‘type’ and ‘ndim’ to check the data type and dimension of the object a.

And then finally we have used the for loop to print the elements of our array.

Following program illustrates how we can implement this-

 import numpy as np
 a=np.array([11,12,13,14,15]) #declaring the array
 print(type(a))
 print(a.ndim)
 #printing the array
 for i in range(5):
     print("The {} element of array is {}".format(i+1,a[i])) 

INPUT-

print in the same line in Python

OUTPUT-

print in the same line in Python

In the above output, we have seen that-

  1. The data type is ‘numpy.ndarray’
  2. The dimension of our array is one.
  3. Then five statements are printed and each statement tells us about the element of the array.

In the following example we will again use the ‘end’ keyword and print all our elements in a single line.

Following program illustrates how we can perform this in Python-

 import numpy as np
 a=np.array([11,12,13,14,15]) #declaring the array
 print(type(a))
 print(a.ndim)
 #printing the array
 for i in range(1):
     print("The elements of array are {}".format(a[i]),end=" ")
 for j in range(1,5):
     print(a[j],end=" ") 

INPUT-

print in the same line in Python

OUTPUT-

print in the same line in Python

This time in the output, our observations are-

  1. The data type is ‘numpy.ndarray’
  2. The dimension of our array is one.
  3. A single statement is printed that displays the elements of our array.

If you find the above code difficult to understand there’s an alternate approach that can implement the same.

     import numpy as np
 a=np.array([11,12,13,14,15]) #declaring the array
 print(type(a))
 print(a.ndim)
 for i in range(5):
     print(a[i],end=" ") 

INPUT-

print in the same line in Python

OUTPUT-

print in the same line in Python

On executing the program, we can see that it displays the same result.

The examples that we have discussed till now take blank space as a parameter in the end.

We can also specify a special character at its place.

In the following examples, we shall see how we can display the output in a single line and add some special characters in between the elements.

In the given example, we have used the ‘#’ as a parameter.

Check the given program-

 import numpy as np
 a=np.array([11,12,13,14,15]) #declaring the array
 print(type(a))
 print(a.ndim)
 for i in range(5):
     print(a[i],end="# ") 

INPUT-

print in the same line in Python

OUTPUT-

print in the same line in Python

In  the above output, we have observed that-

  1. The data type is ‘numpy.ndarray’
  2. The dimension of our array is one.
  3. All the elements have the special character ‘#” after them.

Let us see how we can use some more special characters-

In the given example, we have used the ‘.’ as a parameter.

 import numpy as np
 a=np.array([11,12,13,14,15]) #declaring the array
 print(type(a))
 print(a.ndim)
 for i in range(5):
     print(a[i],end=". ") 

INPUT-

print in the same line in Python

OUTPUT-

print in the same line in Python

In the above output, we have observed that-

  1. The data type is ‘numpy.ndarray’
  2. The dimension of our array is one.
  3. All the elements have the special character ‘.” after them.

In the given example, we have used the ‘*’ as a parameter.   

 import numpy as np
 a=np.array([11,12,13,14,15]) #declaring the array
 print(type(a))
 print(a.ndim)
 for i in range(5):
     print(a[i],end="* ") 

INPUT-

print in the same line in Python

OUTPUT-

print in the same line in Python

In the above output, we have observed that-

  1. The data type is ‘numpy.ndarray’
  2. The dimension of our array is one.
  3. All the elements have the special character ‘*” after them.

Now we will see how we can print a paragraph in Python-

In the first example, all the statements provided in the print function are displayed in separate lines.

Check the given program and see what happens when it is executed-

 print("I love programming in Python language.")
 print("Lists, Tuples and Dictionaries are the data structures in Python.")
 print("We can create data frames in python by importing pandas.")
 print("Visualizations can be done for a better understanding of our data,")
 print("matplotlib and seaborn are the popular libraries for the same.") 

INPUT-

print in the same line in Python

OUTPUT-

print in the same line in Python

All the statements are printed in separate lines in the output.

In the next program, we have used the ‘end’ keyword and printed the statements in the form of a paragraph-

 print("I love programming in Python language.",end=" ")
 print("Lists,Tuples and Dictionaries are the data structures in Python.",end=" ")
 print("We can create dataframes in python by importing pandas.",end=" ")
 print("Visualizations can be done for a better understanding of our data,",end=" ")
 print("matplotlib and seaborn are the popular libraries for the same.") 

INPUT-

print in the same line in Python

OUTPUT-

print in the same line in Python

In the above output, we have seen it is printed in the way we wanted.

So in this article, we discussed various examples in detail of how to print in the same line in Python.


Related Topics

Python Packages

A package is a collection of Python modules that share a similar namespace and are generated by putting all of the modules in a single directory with certain special files...

6 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Anaconda python 3.7 download for windows 10 64-bit

Before installing Anaconda, you need to first know what Anaconda actually is. Anaconda is an IDE (Integrated development environment) platform used to code and executes python programs. Anaconda works both online...

3 minutes read.

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

Python Euclidean Distance

Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for...

1 minute read.

Captcha Code in Python with Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Pascal Triangle

Python Pascal Triangle Pascal triangle A pascal triangle is a number pattern of triangular array of the binomial coefficients. For designing a pascal triangle, we write a function in the program which...

5 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Check Letter in a String Python

Checking a specific letter or a character in a given string or a string which is taken as an input from the user is possible in Python in broadly three...

3 minutes read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

3 minutes read.

Python Static Method

Introduction to methods that are majorly used in Python: Class MethodsInstance MethodsStatic Methods Before going to the in-depth concept of Static Methods, let us briefly discuss Class Methods and Instance Methods. Class Methods The...

4 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.

YOLO Python

YOLO means you only look once.It is a technique to perform object detection is called YOLO. It is the algorithmused by the program to identify items in the image. Earlier detection...

4 minutes read.

How to change a value of a tuple in Python

Python is the language for newly evolving technologies and has become one of the most popular programming languages in the world. It is used in everything right, from simple algorithm...

3 minutes read.

Dynamic Typing in Python

Many key factors for developing a great programming language include how it manages its memory space. This memory space largely depends on empty memory boxes called variables that one creates...

3 minutes read.

Difference between Yield and Return in Python

Python yield statement The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns...

3 minutes read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.

Python float()

Python float() class The float() class in Python returns a floating point number constructed from a number or string x. Syntax class float([x]) Parameter x: This parameter represents a number or a string that can be converted...

1 minute read.

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.