How to print a multiline string centralised (Python)
Question:
I’m trying to print the following string in a centralised position in the console, but .center() doesn’t seem to work
""" , ,
(.-""-.)
| / / /|
| / =. .= / |
( o/o / )/
_, '-/ -' ,_/
v __/ v
__/__/ /
___ |--|/ /___
/` / `
"""
Answers:
The problem is that in your input string you have spaces before your pixel art.
I used also the answer provided in the comments (link).
import shutil
SIZE = shutil.get_terminal_size()
COLUMNS = SIZE.columns
def tty_center_str(s: str):
print(s.center(COLUMNS))
def tty_center_multiline(s: str):
for line in s.splitlines():
tty_center_str(line.strip())
Output:
>>> tty_center_multiline(s)
, ,
(.-""-.)
| / / /|
| / =. .= / |
( o/o / )/
_, '-/ -' ,_/
v __/ v
__/__/ /
___ |--|/ /___
/` / `
I’m trying to print the following string in a centralised position in the console, but .center() doesn’t seem to work
""" , ,
(.-""-.)
| / / /|
| / =. .= / |
( o/o / )/
_, '-/ -' ,_/
v __/ v
__/__/ /
___ |--|/ /___
/` / `
"""
The problem is that in your input string you have spaces before your pixel art.
I used also the answer provided in the comments (link).
import shutil
SIZE = shutil.get_terminal_size()
COLUMNS = SIZE.columns
def tty_center_str(s: str):
print(s.center(COLUMNS))
def tty_center_multiline(s: str):
for line in s.splitlines():
tty_center_str(line.strip())
Output:
>>> tty_center_multiline(s)
, ,
(.-""-.)
| / / /|
| / =. .= / |
( o/o / )/
_, '-/ -' ,_/
v __/ v
__/__/ /
___ |--|/ /___
/` / `