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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Program to cyclically rotate an array by one in Python | List Slicing
- 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
- Python | Reverse Slicing of given string
- String Slicing in Python
- Python | Reverse Interval Slicing String
- Python | Reverse Incremental String Slicing
- Interesting facts about strings in Python | Set 2 (Slicing)
- Python List Comprehension and Slicing
- Basic Slicing and Advanced Indexing in NumPy Python
- Python Slicing | Reverse an array in groups of given size
- Python Slicing | Extract ‘k’ bits from a given position
- Python | Slicing list from Kth element to last element
- Python | Variable list slicing
- Python | Alternate range slicing in list
- Python | Custom slicing in List
- Python | K elements Slicing
- Python | Custom List slicing Sum
- Slicing range() function in Python
- Python - Convert 2D list to 3D at K 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 firstname.lastname@example.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.