How to Print a Horizontal Line in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Print a horizontal line in Python
  2. Print the items in a List horizontally
  3. Print multiple blank lines in Python
  4. Removing the trailing newline character when printing

# 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).
main.py
# ✅ Print a horizontal line print('─' * 25) # 👉️ ──────────── print('⸻' * 25) # 👉️ ⸻⸻⸻⸻⸻ print('⸺' * 25) # 👉️ ⸺⸺⸺⸺⸺⸺⸺⸺ print('*' * 25) # 👉️ ************

print-horizontal-line

The code for this article is available on GitHub

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.

main.py
print('─' * 25) # 👉️ ────────────

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

# Print the items in a list horizontally

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.

main.py
my_list = ['bobby', 'hadz', 'com'] for item in my_list: print(item, end=' ') # 👉️ bobby hadz com

print items in list horizontally

The code for this article is available on GitHub

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'

Alternatively, you can use the iterable unpacking operator.

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

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

# Print multiple blank lines in Python

Use the multiplication operator to print multiple blank lines.

When the multiplication operator is used with a string and an integer, it repeats the string the specified number of times.

main.py
print('before') print('\n' * 3) print('after') # before # after

print multiple blank lines

The code for this article is available on GitHub

We used the multiplication operator to print multiple blank lines.

When the multiplication operator is used with a string and an integer, it repeats the string the specified number of times.
main.py
print('a' * 2) # 👉️ 'aa' print('a' * 3) # 👉️ 'aaa'

The \n character represents a new line in Python.

# Removing the trailing newline character when printing

However, note that the print() function adds a newline (\n) character at the end of each message.

main.py
# 👇️ Adds a newline character automatically print('a', 'b', 'c') # 👉️ 'a b c\n' # 👇️ Set `end` to empty string to remove newline character print('a', 'b', 'c', end='') # 👉️ 'a b c'

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

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

If you need to remove the newline \n character the print() function adds by default, set the end keyword argument to an empty string.

main.py
print('before', end='') print('\n' * 3) print('after', end='') # before # after
The code for this article is available on GitHub

When the end keyword argument is set to an empty string, no newline character is added at the end of the message.

I've also written a detailed guide on how to repeat a string N times.

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