How to Print on the Same Line in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Print on the same line in Python
  2. Printing on the same line with the iterable unpacking operator
  3. Printing on the same line using str.join()
  4. How to print() and input() on the same line in Python

# Print on the same line in Python

To print on the same line:

  1. Use a for loop to iterate over the sequence.
  2. Call the print() function with each value and set the end argument to a space.
  3. For example, print(item, end=' ').
main.py
my_list = ['bobby', 'hadz', 'com'] # โœ… print list items horizontally in for loop for item in my_list: print(item, end=' ') # ๐Ÿ‘‰๏ธ bobby hadz com # ------------------------------------------- # โœ… print horizontally using iterable unpacking print(*my_list) # ๐Ÿ‘‰๏ธ bobby hadz com # ------------------------------------------- # โœ… print horizontally using str.join() result = ' '.join(my_list) print(result) # ๐Ÿ‘‰๏ธ bobby hadz com

print on same line

The code for this article is available on GitHub

The first example uses a for loop and the end argument of the print() function to print on the same line.

main.py
my_list = ['bobby', 'hadz', 'com'] for item in my_list: print(item, end=' ') # ๐Ÿ‘‰๏ธ bobby hadz com

The end argument is printed at the end of the message.

By default, end is set to a newline character (\n).

main.py
print('a', 'b', 'c') # ๐Ÿ‘‰๏ธ 'a b c\n' print('a', 'b', 'c', end='') # ๐Ÿ‘‰๏ธ 'a b c'

We set the argument to a space to print the list items horizontally, separated by spaces.

# Printing on the same line with the iterable unpacking operator

Alternatively, you can use the iterable unpacking operator.

The iterable unpacking operator will unpack the items of the iterable in the call to the print() function.

main.py
my_list = ['bobby', 'hadz', 'com'] print(*my_list) # ๐Ÿ‘‰๏ธ bobby hadz com

print on same line using iterable unpacking operator

The code for this article is available on GitHub

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

You can imagine that the items of the list get passed as multiple, comma-separated arguments to the print() function.

By default, the items of the iterable get printed horizontally, separated by spaces.

You can pass the sep keyword argument to the print() function to change the separator.

main.py
# ๐Ÿ‘‡๏ธ without separator print(*my_list, sep='') # ๐Ÿ‘‰๏ธ bobbyhadzcom # ๐Ÿ‘‡๏ธ with hyphen separator print(*my_list, sep='-') # ๐Ÿ‘‰๏ธ bobby-hadz-com

The sep argument is the separator between the arguments we pass to print().

By default, the argument is set to a space.

Alternatively, you can use the str.join() method.

# Printing on the same line using str.join()

This is a two-step process:

  1. Use the str.join() method to join the items of the iterable into a string.
  2. Use the print() function to print the string.
main.py
# โœ… Print a list of strings horizontally my_list = ['bobby', 'hadz', 'com'] result = ' '.join(my_list) print(result) # ๐Ÿ‘‰๏ธ bobby hadz com # --------------------------------------- # โœ… Print a list of numbers horizontally my_list = [2, 4, 8] result = ' '.join(str(number) for number in my_list) print(result) # ๐Ÿ‘‰๏ธ 2 4 8

print on same line using str join

The code for this article is available on GitHub

We used the str.join() method to join the items in the list into a string.

The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your iterable contains numbers or other types, convert all of the values to strings before calling join().

main.py
my_list = [2, 4, 8] result = ' '.join(str(number) for number in my_list) print(result) # ๐Ÿ‘‰๏ธ 2 4 8

The string the method is called on is used as the separator between the elements.

We used a space in the examples, but you can use any other separator.

main.py
my_list = ['bobby', 'hadz', 'com'] result = '-'.join(my_list) print(result) # ๐Ÿ‘‰๏ธ bobby-hadz-com

# How to print() and input() on the same line in Python

Pass a string to the input() function to have a print statement and input on the same line.

main.py
# โœ… Print input message on the same line username = input('Enter your username: ') print(username) # ---------------------------------- # โœ… Use print() and input() without separator and newline character print( 'Your username is: ', input('Enter your username: '), sep='', end='' )
The code for this article is available on GitHub

In the first example, we passed a prompt string to the input() function.

print and input on same line

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

main.py
s = input('Enter your name: ') print(s)

The function then reads the line from the input, converts it to a string and returns the result.

# Using the print() function before or after input()

You can also use the print() function before or after the input() function.

main.py
print('this runs before') user_input = input('Enter your username: ') print('this runs after')
The code for this article is available on GitHub

If you use the print() function instead of the prompt argument, the print statement is printed on a separate line.

main.py
print('Enter your name: ') s = input() print(s)

print input separate

If you want to call the print() function with multiple arguments without a separator, set the sep and end keyword arguments to empty strings.

main.py
print( 'Your username is: ', input('Enter your username: '), sep='', end='' )

print input sep end

The sep argument is the separator between the arguments we pass to print().

By default, the argument is set to a space.

main.py
print('a', 'b', 'c') # ๐Ÿ‘‰๏ธ 'a b c' print('a', 'b', 'c', sep='') # ๐Ÿ‘‰๏ธ 'abc'

The end argument is printed at the end of the message.

By default, end is set to a newline character (\n).

main.py
print('a', 'b', 'c') # ๐Ÿ‘‰๏ธ 'a b c\n' print('a', 'b', 'c', end='') # ๐Ÿ‘‰๏ธ 'a b c'

You can also use a formatted string literal to print multiple arguments on the same line.

main.py
print(f'Your name is: {input("Enter your name: ")}')

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐Ÿ‘‰๏ธ is subscribed: True

Make sure to wrap expressions in curly braces - {expression}.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev