Print a horizontal line in Python


Borislav Hadzhiev

Last updated: Sep 5, 2022


Photo from Unsplash

Print a horizontal line in Python #

To print a horizontal line:

  1. Use the multiplication operator to repeat a hyphen N times.
  2. Use the print() function to print the horizontal line.
  3. For example, print('─' * 25).
# ✅ print a horizontal line print('─' * 25) # 👉️ ──────────── print('⸻' * 25) # 👉️ ⸻⸻⸻⸻⸻ print('⸺' * 25) # 👉️ ⸺⸺⸺⸺⸺⸺⸺⸺ print('*' * 25) # 👉️ ************ # -------------------------------- # ✅ print the items in a list horizontally my_list = ['bobby', 'hadz', 'com'] for item in my_list: print(item, end=' ') # 👉️ bobby hadz com print(*my_list) # 👉️ bobby hadz com


The examples use the multiplication operator to print a horizontal line.

When the multiplication operator is used with a string and an integer, the string is repeated the specified number of times.
print('─' * 25) # 👉️ ────────────

You can use this approach to print a horizontal line that consists of any character.

If you need to print the items in an iterable horizontally, set the end argument of the print() function to a string containing a space.
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).
print('a', 'b', 'c') # 👉️ 'a b c\n' print('a', 'b', 'c', end='') # 👉️ 'a b c'

Alternatively, you can use the iterable unpacking operator.
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 use this approach to print the items of an iterable with any separator, it doesn't have to be a space.
my_list = ['bobby', 'hadz', 'com'] 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.

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.