Given a string of size n, write functions to perform following operations on string.
- Left (Or anticlockwise) rotate the given string by d elements (where d <= n).
- Right (Or clockwise) rotate the given string by d elements (where d <= n).
Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeeksforGee" Input : s = "qwertyu" d = 2 Output : Left rotation : "ertyuqw" Right rotation : "yuqwert"
- Separate string in two parts first & second, for Left rotation Lfirst = str[0 : d] and Lsecond = str[d :]. For Right rotation Rfirst = str[0 : len(str)-d] and Rsecond = str[len(str)-d : ].
- Now concatenate these two parts second + first accordingly.
Left Rotation : eksforGeeksGe Right Rotation : ksGeeksforGee
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- String slicing in Python to check if a string can become empty by recursive deletion
- String Slicing in Python
- Python | Reverse Slicing of given string
- Python | Reverse Incremental String Slicing
- Python | Get the substring from given string using list slicing
- Python | Reverse Interval Slicing String
- Program to cyclically rotate an array by one in Python | List Slicing
- Python | Check if given string can be formed by concatenating string elements of list
- Python - Remove front K characters from each string in String List
- Python | Merge Tuple String List values to String
- Python | Sorting string using order defined by another string
- Python | Check if string ends with any string in given list
- Python | Delimited String List to String Matrix
- Python - Length of shortest string in string list
- Python | Check if a given string is binary string or not
- Python | Sort each String in String list
- Python - Remove String from String List
- Python | K elements Slicing
- Slicing range() function in Python
- Python List Comprehension and Slicing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.