String slicing in Python to Rotate a String Last Updated : 05 Mar, 2025 Comments Improve Suggest changes Like Article Like Report 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).For example, let's take a string s = "GeeksforGeeks" and d = 2, so for this example, our Left Rotation will be "eksforGeeksGe" and Right Rotation will be "ksGeeksforGee". Let's discuss some of the ways to do it with examples.Using SlicingPython’s string slicing provides a quick way to rotate strings. The idea is to divide the string into two parts and rearrange them to get the desired rotation. Python s = "GeeksforGeeks" d = 2 # Left Rotation left = s[d:] + s[:d] # Right Rotation right = s[-d:] + s[:-d] print("Left Rotation:", left) print("Right Rotation:", right) OutputLeft Rotation: eksforGeeksGe Right Rotation: ksGeeksforGee Explanation: Left Rotation: Split into s[:d] (first part) and s[d:] (second part), then concatenate in reverse order.Right Rotation: Split into s[:-d] (first part) and s[-d:] (second part), then concatenate.By Extending the String Create an extended string by concatenating 's + s' as it will cointain all possible rotations and then extract the rotated substring by slicing within the extended string. Python s = "GeeksforGeeks" d = 2 # Create extended string ext_s = s + s n = len(s) # Left Rotation left = ext_s[d:n + d] # Right Rotation right = ext_s[n - d: 2 * n - d] print("Left Rotation:", left) print("Right Rotation:", right) OutputLeft Rotation: eksforGeeksGe Right Rotation: ksGeeksforGee Explanation:extracting a substring from index 'd to d+n' using ext_s[d:n + d] (n = length of string) will get us the 'Left Rotation'.similarly, extracting a substring from index 'n-d to 2n-d' using ext_s[n - d: 2 * n - d] (n = length of string) will get us the 'Right Rotation'. Comment More infoAdvertise with us Next Article How to rotate text in Matplotlib â Python S Shashank Mishra Follow Improve Article Tags : Python rotation python-string Python string-programs Practice Tags : python Similar Reads How to reverse a String in Python Reversing a string is a common task in Python, which can be done by several methods. In this article, we discuss different approaches to reversing a string. One of the simplest and most efficient ways is by using slicing. Letâs see how it works:Using string slicingThis slicing method is one of the s 4 min read Build an Application for Screen Rotation Using Python In this article, we are going to write a python script for screen rotation and implement it with GUI. The display can be modified to four orientations using some methods from the rotatescreen module, it is a small Python package for rotating the screen in a system. Installation:pip install rotate-s 2 min read String Translate using Dict - Python In Python, translating a string based on a dictionary involves replacing characters or substrings in the string with corresponding values from a dictionary. For example, if we have a string "python" and a dictionary {"p": "1", "y": "2"}, we can translate the string to "12thon". Letâs explore a few m 3 min read Reverse Words in a Given String in Python In this article, we explore various ways to reverse the words in a string using Python. From simple built-in methods to advanced techniques like recursion and stacks. We are going to see various techniques to reverse a string.Using split() and join()Using split() and join() is the most common method 2 min read How to rotate text in Matplotlib â Python In this article, we will learn how to rotate text in Matplotlib in Python. Out of the many parameters in matplotlib.text(), there are three main parameters that will define text rotation and how the text will be rotated. Syntax of matplotlib.text method Syntax: matplotlib.text(x=0, y=0, text='', rot 2 min read How to rotate text in Matplotlib â Python In this article, we will learn how to rotate text in Matplotlib in Python. Out of the many parameters in matplotlib.text(), there are three main parameters that will define text rotation and how the text will be rotated. Syntax of matplotlib.text method Syntax: matplotlib.text(x=0, y=0, text='', rot 2 min read Like