How to print horizontally in Python

avatar

Borislav Hadzhiev

Last updated: Sep 5, 2022

banner

Photo from Unsplash

Printing horizontally in Python #

To print horizontally:

  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

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

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.

Alternatively, you can use the iterable unpacking operator.

Printing horizontally using iterable unpacking #

Use the iterable unpacking operator to print horizontally, e.g. print(*my_list). The iterable unpacking operator will unpack the items of the iterable in the call to the print() function. The print() function will print the values horizontally.

main.py
my_list = ['bobby', 'hadz', 'com'] print(*my_list) # 👉️ bobby hadz com

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 horizontally using str.join() #

To print horizontally:

  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

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