Bl4ck_B0y
Bl4ck_B0y

Reputation: 53

How to line break a multi-line sentence

I'm making a ASCII-art generator. I won't get into detail how does it work, but in the end, I have one string, separated with "\n" whenever I want to print another line. Example for input "ABC":

art="░█▀▀█ ▒█▀▀█ ▒█▀▀█\n▒█▄▄█ ▒█▀▀▄ ▒█░░░\n▒█░▒█ ▒█▄▄█ ▒█▄▄█"
print(art)

But in case of longer sentences (which wouldn't fit in the terminal) the whole art breaks, because in the first line it prints part of arts first line, then in the second line - remaining part of arts first line. So it comes out broken. Is there a way to somehow make a multi-line line break or something to do it correctly?

Upvotes: 0

Views: 114

Answers (1)

I defined a fixed number of letters per line (N_max = 14 in the example)

You can try this:

from math import ceil

N_max = 14 
art="░█▀▀█ ▒█▀▀█ ▒█▀▀█\n▒█▄▄█ ▒█▀▀▄ ▒█░░░\n▒█░▒█ ▒█▄▄█ ▒█▄▄█"

arts = art.split("\n")
arts = [a.split(" ") for a in arts]
art_len = len(arts[0])

for k in range(ceil(art_len/N_max)):
    for art in arts:
        for i, a in enumerate(art[k*N_max:]):
            if(i>=N_max or i+k*N_max >= art_len): break
            print(a, end = '')
        print('')
    print('')

Not the best solution, but it worked here.

If you want, you can use this art with 36 letters to test:

art="░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█ ░█▀▀█ ▒█▀▀█ ▒█▀▀█\n▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░ ▒█▄▄█ ▒█▀▀▄ ▒█░░░\n▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█"

Upvotes: 1

Related Questions