Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
How should I change the order of a string?
a = 'abcdefg'
and I want to put the last character of the string at beginning every time when I iterate through the string
a = 'gabcdef' a = 'fgabcde` etc
a=a[-1]+a[:-1]
You can try this:-
a = a[-1] + a[:-1]
Add a comment
You could use a deque as an alternative to slicing:
deque
from collections import deque a = 'abcdefg' d = deque(a) for _ in range(len(a)): d.rotate() print(''.join(d))
Output:
gabcdef fgabcde efgabcd defgabc cdefgab bcdefga abcdefg
You could add something like a = a[-1] + a[:-1]
a = 'abcdefg' for i in range(len(a)): a = a[-1] + a[:-1] print(a) >>gabcdef >>fgabcde >>efgabcd >>defgabc >>cdefgab >>bcdefga >>abcdefg
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
a=a[-1]+a[:-1]