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
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Get the substring from given string using list slicing
- 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 | Check if string ends with any string in given list
- Python | Check if a given string is binary string or not
- Python List Comprehension and Slicing
- Python | Variable list slicing
- Python Slicing | Extract ‘k’ bits from a given position
- Interesting facts about strings in Python | Set 2 (Slicing)
- Python | Alternate range slicing in list
- Python Slicing | Reverse an array in groups of given size
- Basic Slicing and Advanced Indexing in NumPy Python
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Create a new string by alternately combining the characters of two halves of the string in reverse
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.