Home>

I want to make a triangle of the character string on the right side by inputting the character string and the number of columns in Python.
The input character strings are arranged from left to right in the bottom row, and if it is cut off in the middle, the continuation of the character string is output in the upper row, and it feels like it is repeated up to the top row.
Height: 12
String: PYTHON
Output: Figure below

What I tried

After trying various things, I happened to write a code like this. However, in this case, if the character string is cut off in the middle and goes to the upper row, it will be reset (?), So how can I continue it? (I'm sorry if I made a mistake from the ground up.)

word = str (input ("Enter the word:"))
height = int (input ("Enter the height:"))
for x in range (len (word)):
    word + = word
for i in range (1, height + 1):
    for j in range (1, height + 1):
        if (j<= height --i):
            print ('', end ='')
        else: else:
            print (word [height + j], end =''')
    print ("")


Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    p.py

    word = str (input ("Enter the word:"))
    height = int (input ("Enter the height:"))
    TOTAL_LEN = height * (height + 1) // 2
    WORD_LEN = len (word)
    word * = (height // len (word)) + 1
    padding = "" * height
    for y in range (1, height + 1):
      pos = (TOTAL_LEN --y * (y + 1) // 2)% WORD_LEN
      print (padding [y:] + word [pos: pos + y])

    Execution example

  • Answer # 2

    word = str (input ("Enter the word:"))
    height = int (input ("Enter the height:"))
    wordlength = sum ([i for i in range (height)])
    word = word * (wordlength // len (word) + 2)
    s = 0
    for i in range (height):
        print ('''* (height --i + 1), end ='')
        print (word [s: s + i + 1])
        s = s + i + 1
    Enter the word: python
    Enter the height: 12
                 p
                yt
               hon
              pyth
             onpyt
            honpyt
           honpyth
          onpython
         pythonpyt
        honpythonp
       ythonpython
      pythonpython