Hướng dẫn print horizontal line python

I am trying to print a horizontal line in python. basically I want to print:

actual

"----------"

Expected

but I want the '----' to be continuous. Any suggestions?

asked Jan 4, 2021 at 10:39

4

Try printing out using the following:

print('─' * 10)  # U+2500, Box Drawings Light Horizontal
print('─' * 10)  # U+2501, Box Drawings Heavy Horizontal
print('―' * 10)  # U+2015, Horizontal Bar
print('_' * 10)  # Underscore

Output

──────────   U+2500, Box Drawings Light Horizontal
──────────   U+2501, Box Drawings Heavy Horizontal
――――――――――   U+2015, Horizontal Bar
__________   Underscore

Take a look at the Box-drawing character Wikipedia page for more bars you can use

answered Jan 4, 2021 at 10:50

1

If you would like to print a horizontal line across the terminal the width of the terminal, you can use:

import os
term_size = os.get_terminal_size()
print('=' * term_size.columns)

answered May 21, 2021 at 18:53

Hướng dẫn print horizontal line python

1

You could try printing extended ASCII characters as in the below example. Here the ASCII value u'\u2500' relates to hyphen without spaces at start and end. The \u specifies the following string is in extended ASCII form and 2500 denotes the symbol.

print(u'\u2500' * 10)

Result

──────────

answered Jan 4, 2021 at 10:55

SwadhikarSwadhikar

2,0131 gold badge18 silver badges32 bronze badges

5

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

Copied!

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

Hướng dẫn print horizontal line python

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.

Copied!

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.

Copied!

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

Copied!

print('a', 'b', 'c') # 👉️ 'a b c\n' print('a', 'b', 'c', end='') # 👉️ 'a b c'

Alternatively, you can use the iterable unpacking operator.

Copied!

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.

Copied!

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.