0

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?

4
  • 1
    Any reason why this multiline has to be held in a string? Wouldn't a list be more appropriate? Something you can iterate and print line by line?
    – JNevill
    Commented Jul 26, 2022 at 16:19
  • I would suggest using triple quote strings, as that will let you see multiple lines easily in the editor and you don't have to type out the \n. Also, you would have to implement your own multi-line line break. Maybe figuring out the terminal size, adding the maximum amount of words into the terminal that fit on one line, then printing out those words before moving onto the next words in the list.
    – Bob th
    Commented Jul 26, 2022 at 16:24
  • Thinking of this like a list: print(*art.split('\n'), sep = '\n')
    – JNevill
    Commented Jul 26, 2022 at 16:25
  • Could you clarify what you want to do? Give the example of a longer sentence that breaks, and show what you would want to show instead of the broken line?
    – pho
    Commented Jul 26, 2022 at 16:25

1 Answer 1

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▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█ ▒█░▒█ ▒█▄▄█ ▒█▄▄█"

2
  • I tried with 36 letters and it worked Commented Jul 27, 2022 at 1:25
  • Perfect! That's exactly what I was looking for. Very clever to use this line: "for k in range(ceil(art_len/N_max)):"!
    – Bl4ck_B0y
    Commented Jul 27, 2022 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.