Home > Blockchain >  How to rotate a string using for/while loops in Python?
How to rotate a string using for/while loops in Python?

Time:09-02

I want to rotate the string so that its last character goes on the first place and so on.

word = "Burrow"

Sample Output:

Burrow
wBurro
owBurr
rowBur
rrowBu
urrowB

So far I can rotate the string in the opposite direction. Here's what I have done:

def main():
    word = "Burrow"
    a = word
    for i in range(len(word)):
        c = a[i:]   a[:i]
        print(c)

if __name__ == "__main__":
    main()

Burrow
urrowB
rrowBu
rowBur
owBurr
wBurro

Cannot find out what to do to get the desirable output. Tried negative indices but never succeeded.

CodePudding user response:

You're on the right track, but you need to reverse the order that you iterate over the characters in the input word. You want to start at the end and go backwards.

def main():
    word = "Burrow"
    a = word
    for i in range(len(word), 0, -1):
        c = a[i:]   a[:i]
        print(c)

if __name__ == "__main__":
    main()

Output:

Burrow
wBurro
owBurr
rowBur
rrowBu
urrowB

The change was in range(len(word), 0, -1), which starts the range at len(word) and counts backwards towards 0.

CodePudding user response:

Try this:


def main(word):
    l = len(word)
    out = list(word)

    for a in range(l):
        print(''.join(out[:l]))
        out.insert(0,out[l-1])

word = "Burrow"
if __name__ =='__main__':
    main(word)


output

Burrow
wBurro
owBurr
rowBur
rrowBu
urrowB

CodePudding user response:

Remember that negative indices in python correspond to counting backwards from the end of your string / list, and think about what index you want to break your string at. Also note that your first slice starts at the index that your second slice ends, since slices exclude the end index.

Iteration (i) Expected output Num letters from end to rotate around
0 Burrow 0
1 wBurro 1
2 owBurr 2
3 rowBur 3
... ...

Notice a pattern? The first part of your output is the last i letters in the ith iteration, so should start at the index -i and extend to the end. This means your slice would be [-i:]. The second part of your output ends at the start of your first part, so your slice would be [:-i]

So do this:

word = "Burrow"
a = word
for i in range(len(word)):
    c = a[-i:]   a[:-i]
    print(c)

Which outputs as expected:

Burrow
wBurro
owBurr
rowBur
rrowBu
urrowB

CodePudding user response:

Try this:

def main():
    word = "Burrow"
    a = word
    c = ''
    for i in range(len(word)):
        c = a if i == 0 else a[-1]   a[0:-1]
        print(c)
        a=c

if __name__ == "__main__":
    main()

CodePudding user response:

This will work for you.

def main():
    word = "Burrow"
    print(word)
    for i in range(len(word) - 1):
        word = word[-1]   word[:-1]
        print(word)

if __name__ == "__main__":
     main()

CodePudding user response:

Try this :

  1. If you don't want to change the original variable word Solution : Create new word with index manipulation in each loop
word = "Burrow"

for i in range(len(word)-1):
    new_word = word[-1-i:]   word[:len(word)-1-i]
    print(new_word)
  1. Allowed to change the variable word itself Solution : In each iteration over the characters of word, keep on replacing the first and last index of the word
word = "Burrow"

for i in range(len(word)-1):
    word = word[-1]   word[:-1]
    print(word)
  • Related