Scrolling text. Rearrange the character. Characters in a string from index zero to last. I want to get the following result

Question:

def scrolling_text(string: str) -> list:
    new_list = []
    string = string.upper()
    for i in range(0, len(string)):
        string = string[i:] + string[:i]
        new_list.append(string)

    return new_list


print(scrolling_text('robot'))   # ["ROBOT", "OBOTR", "BOTRO", "OTROB", "TROBO"]
Asked By: Andrey Petrus

||

Answers:

What you want to do to get the list is shift the first letter of the string and append it to the back of the remaining sub-string through slicing:

def scrolling_text(string: str) -> list:
  new_list = []
  string = string.upper()
  for i in range(0, len(string)):
      if i != 0:
        string = string[1:] + string[:1]
      new_list.append(string)

  return new_list


print(scrolling_text('robot'))  # ["ROBOT", "OBOTR", "BOTRO", "OTROB", "TROBO"]
Answered By: Marcelo Paco
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.