The Wayback Machine - https://web.archive.org/web/20230512160956/https://www.geeksforgeeks.org/string-slicing-python-rotate-string/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

String slicing in Python to rotate a string

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given a string of size n, write functions to perform following operations on string.

  1. Left (Or anticlockwise) rotate the given string by d elements (where d <= n).
  2. Right (Or clockwise) rotate the given string by d elements (where d <= n).

Examples:

Input : s = "GeeksforGeeks"
        d = 2
Output : Left Rotation  : "eksforGeeksGe" 
         Right Rotation : "ksGeeksforGee"  


Input : s = "qwertyu" 
        d = 2
Output : Left rotation : "ertyuqw"
         Right rotation : "yuqwert"

Method 1: We have existing solution for this problem please refer Left Rotation and Right Rotation of a String link. We will solve this problem quickly in python using String Slicing. Approach is very simple,

  1. 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 : ].
  2. Now concatenate these two parts second + first accordingly.

Implementation:

Python3




# Function to rotate string left and right by d length
 
def rotate(input,d):
 
    # slice string in two parts for left and right
    Lfirst = input[0 : d]
    Lsecond = input[d :]
    Rfirst = input[0 : len(input)-d]
    Rsecond = input[len(input)-d : ]
 
    # now concatenate two parts together
    print ("Left Rotation : ", (Lsecond + Lfirst) )
    print ("Right Rotation : ", (Rsecond + Rfirst))
 
# Driver program
if __name__ == "__main__":
    input = 'GeeksforGeeks'
    d=2
    rotate(input,d)

Output:

Left Rotation  : eksforGeeksGe 
Right Rotation : ksGeeksforGee

Method 2: We use extended string to rotate the string. We will solve this problem quickly in python by slicing extended string. Approach is very simple,

Use extended string Extend_str, for Left rotation Lfirst = Extended_str[n : l1+n] . For Right rotation Rfirst = str[l1-n : l2-n].
Now print this string. 

Implementation:

Python3




# Function to rotate string left and right by d length
 
def rotate(str1,n):
 
    # Create the extended string and index of for rotation
    temp = str1 + str1
    l1 = len(str1)
    l2 = len(temp)
    Lfirst = temp[n  : l1+n]
    Lfirst = temp[l1-n : l2-n]
 
    # now printing the string
    print ("Left Rotation : ", Lfirst)
    print ("Right Rotation : ", Lfirst )
 
# Driver program
if __name__ == "__main__":
    input = 'GeeksforGeeks'
    d=2
    rotate(input,d)

Output

Left Rotation :  ksGeeksforGee
Right Rotation :  ksGeeksforGee

Approach#3: Using deque

Take the input string s and the number of rotations d. Create a deque from the string s. Rotate the deque by d positions to the left or right using the rotate() method. Convert the deque back to a string using join() method and return the rotated string.

Algorithm

1. Take the input string s and the number of rotations d.
2. Create a deque from the string s.
3. If d is positive, rotate the deque to the left by d positions using the rotate() method.
  If d is negative, rotate the deque to the right by -d positions using the rotate() method.
4. Convert the deque back to a string using join() method and return the rotated string.
 

Python3




from collections import deque
 
def rotate_string(s, d):
    deq = deque(s)
    if d > 0:
        deq.rotate(-d)
    else:
        deq.rotate(abs(d))
    return ''.join(deq)
 
s = 'GeeksforGeeks'
d = 2
 
left_rotated = rotate_string(s, d)
right_rotated = rotate_string(s, -d)
 
print("Left Rotation: ", left_rotated)
print("Right Rotation: ", right_rotated)

Output

Left Rotation:  eksforGeeksGe
Right Rotation:  ksGeeksforGee

Time complexity: O(n), where n is the length of the string s.
Auxiliary Space: O(n), where n is the length of the string s.


My Personal Notes arrow_drop_up
Last Updated : 07 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials