Sometimes, while working with Python Strings, we can have problem in which we have both right and left rotate count of characters in String and would like to know the resultant condition of String. This type of problem occurs in competitive programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using String multiplication + string slicing
The combination of above functions can be used to perform this task. In this, we multiple string thrice, perform the concatenation and selectively slice string to get required result.
# Python3 code to demonstrate working of # Right and Left Shift characters in String # Using String multiplication + string slicing # initializing string test_str = 'geeksforgeeks' # printing original string print("The original string is : " + test_str) # initializing right rot r_rot = 7 # initializing left rot l_rot = 3 # Right and Left Shift characters in String # Using String multiplication + string slicing res = (test_str * 3)[len(test_str) + r_rot - l_rot : 2 * len(test_str) + r_rot - l_rot] # printing result print("The string after rotation is : " + str(res)) |
The original string is : geeksforgeeks The string after rotation is : sforgeeksgeek
Method #2 : Using % operator and string slicing
The combination of above functionalities can also be used to perform this task. In this, we find the mod of rotation difference with length to compute the string position.
# Python3 code to demonstrate working of # Right and Left Shift characters in String # Using % operator and string slicing # initializing string test_str = 'geeksforgeeks' # printing original string print("The original string is : " + test_str) # initializing right rot r_rot = 7 # initializing left rot l_rot = 3 # Right and Left Shift characters in String # Using % operator and string slicing temp = (r_rot - l_rot) % len(test_str) res = test_str[temp : ] + test_str[ : temp] # printing result print("The string after rotation is : " + str(res)) |
The original string is : geeksforgeeks The string after rotation is : sforgeeksgeek
Recommended Posts:
- Python - Remove front K characters from each string in String List
- Python - Key with all Characters in String
- Python | Extract characters except of K string
- Python - String with most unique characters
- Iterate over characters of a string in Python
- Python | Get positional characters from String
- Python - String uncommon characters
- Python | Extract only characters from given string
- Python | Convert a list of characters into a string
- Python | Removing unwanted characters from string
- Python | Ways to check string contain all same characters
- Python | Splitting string to list of characters
- Python | Splitting string to list of characters
- Python | Split multiple characters from string
- Python - Characters Index occurrences in String
- Python | Check if frequencies of all characters of a string are different
- Concatenated string with uncommon characters in Python
- Check if both halves of the string have same set of characters in Python
- Python | Return lowercase characters from given string
- Python | Split string into list of characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

