×

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 print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Python Matrix Multiplication

One of the most fundamental mathematical structures, matrices are used often in many disciplines, including mathematics, physics, engineering, computer science, etc. For example, matrices and associated operations (multiplication and addition) are...

8 minutes read.

Python type() Function

Python type() Function The type() function in Python returns the type of an object. The return value is a type object and generally the same object as returned by object.__class__. Syntax class type(object)      ...

1 minute read.

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

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

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.

Isreal() Python

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

4 minutes read.

Python all() Function

Python all() Function The all() function in Python returns a Boolean value ‘True’ if all the items in iterable are true or if the iterable object is empty, else it returns False. Syntax all(iterable) Parameter Iterable: An iterable object...

1 minute read.

Python Empty Tuple

How to Create an Empty Tuple Tuple A tuple is a data structure used to store non-homogeneous data elements. These non-homogeneous data elements consist of integer data type, character data type, String...

3 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

Creating new Database using Python MySQL

In this article, we are going to discuss how to create a new database by connecting Python and MySQL. What is a Database? The places or memory used to secure highly and...

6 minutes read.

Best app for python

What is python? Python is a prevalent, general-purpose, and high-level programming language. Python Programming Language is appropriate for experienced programmers and also for beginners. Python programming language is being utilized by...

19 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

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

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Python Tuple count() Method

Python Tuple count() Method The tuple.count() method in Python returns the number of times a specified value appears in the tuple. Syntax tuple.count(value) Parameter value- This parameter represents the element to search in the tuple Return This...

1 minute read.

ER diagram of the Bank Management System in python

What is an ER diagram? The full form of the ER diagram is the Entity Relationship diagram. This diagram is used in database management systems to have a rough idea of...

3 minutes read.

Python Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

Python lambda() Function

In this tutorial, we will learn about a new concept known as the Lambda function. It is a relatively new concept while learning Python. Python lambda functions are anonymous functions. It...

3 minutes read.