Here is one way:
def rotate(strg, n):
return strg[n:] + strg[:n]
rotate('HELLO', -1) # 'OHELL'
Alternatively, collections.deque
(“double-ended queue”) is optimised for queue-related operations. It has a dedicated rotate() method:
from collections import deque
items = deque('HELLO')
items.rotate(1)
''.join(items) # 'OHELL'
Related Contents:
- How to convert string representation of list to a list?
- Split string every nth character?
- Best way to convert string to bytes in Python 3?
- Convert hex string to int in Python
- Changing one character in a string
- Pandas filtering for multiple substrings in series
- Print string to text file
- Create Pandas DataFrame from a string
- How to use string.replace() in python 3.x
- How to un-escape a backslash-escaped string?
- removing emojis from a string in Python
- How to convert string to binary?
- How do I trim whitespace?
- Find the nth occurrence of substring in a string
- Why is it string.join(list) instead of list.join(string)?
- pandas dataframe str.contains() AND operation
- How do I trim whitespace from a string?
- Reading a text file and splitting it into single words in python
- Remove quotes from String in Python
- Find longest repetitive sequence in a string
- How to extract an IP address from an HTML string?
- Count consecutive characters
- Find index of last occurrence of a substring in a string
- Determine prefix from a set of (similar) strings
- Convert hex string to integer in Python
- Why are python strings and tuples are made immutable?
- Math operations from string [duplicate]
- Finding occurrences of a word in a string in python 3
- Does Python do slice-by-reference on strings?
- Time complexity of string concatenation in Python [duplicate]
- How to explain the reverse of a sequence by slice notation a[::-1]
- Inserting the same value multiple times when formatting a string
- Removing list of words from a string
- Best way to parse a URL query string
- Splitting a string by list of indices
- shuffle string in python
- Python 3.1.1 string to hex
- Print very long string completely in pandas dataframe
- How can I check if a string contains ANY letters from the alphabet?
- Change a string of integers separated by spaces to a list of int
- Split string on commas but ignore commas within double-quotes?
- Why do I get “TypeError: not all arguments converted during string formatting” trying to substitute a placeholder like {0} using %?
- Why do I get “TypeError: not all arguments converted during string formatting” trying to check for an even/odd number?
- How can I use f-string with a variable, not with a string literal?
- How to sort the letters in a string alphabetically in Python
- Escape double quotes for JSON in Python
- how to do bitwise exclusive or of two strings in python?
- Python: How to check a string for substrings from a list? [duplicate]
- How do I get str.translate to work with Unicode strings?
- Remove whitespace in Python using string.whitespace