Word rotation program in python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save 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

    def rotate(input,d):

        Lfirst = input[0 : d]

        Lsecond = input[d :]

        Rfirst = input[0 : len(input)-d]

        Rsecond = input[len(input)-d : ]

        print ("Left Rotation : ", (Lsecond + Lfirst) )

        print ("Right Rotation : ", (Rsecond + Rfirst))

    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

    def rotate(str1,n):

        temp = str1 + str1

        l1 = len(str1)

        l2 = len(temp)

        Lfirst = temp[n  : l1+n]

        Lfirst = temp[l1-n : l2-n]

        print ("Left Rotation : ", Lfirst)

        print ("Right Rotation : ", Lfirst )

    if __name__ == "__main__":

        input = 'GeeksforGeeks'

        d=2

        rotate(input,d)

    Output

    Left Rotation :  ksGeeksforGee
    Right Rotation :  ksGeeksforGee



    A string is given, our task is to slicing the string into two way. One is clockwise and another anticlockwise.

    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).

    Example

    Input: string = "pythonprogram"
    d = 2
    Output: Left Rotation: thonprogrampy
    Right Rotation: ampythonprogr

    Algorithm

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

    Example Code

    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 : ]
       print ("Left Rotation : ", (Lsecond + Lfirst) )
          print ("Right Rotation : ", (Rsecond + Rfirst) )
          # Driver program
       if __name__ == "__main__":
          str = input("Enter String ::>")
    d=2
    rotate(str,d)

    Output

    Enter String ::> pythonprogram
    Left Rotation: thonprogrampy
    Right Rotation: ampythonprogr

    Word rotation program in python

    Updated on 23-Jun-2020 16:09:07

    • Related Questions & Answers
    • String slicing in C# to rotate a string
    • Rotate String in Python
    • String slicing in Python to check if a can become empty by recursive deletion
    • Program to rotate a string of size n, n times to left in Python
    • Python - Ways to rotate a list
    • Program to find a good string from a given string in Python
    • Program to reverse a list by list slicing in Python
    • Python List Comprehension and Slicing?
    • Alternate range slicing in list (Python)
    • A unique string in Python
    • How to reverse a string in Python?
    • How to capitalize a string in Python?
    • How to split a string in Python
    • String Transforms Into Another String in Python
    • Rotate Array in Python

    How do you rotate a word in Python?

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

    How do you rotate words in a string?

    Method#1: A Simple Solution is to use a temporary string to do rotations. For left rotation, first, copy last n-d characters, then copy first d characters in order to the temporary string. For right rotation, first, copy last d characters, then copy n-d characters.

    How do you create a rotating list in Python?

    Let's discuss different ways we can rotate a list in Python..
    Method 1: Rotate a list using Slicing..
    Method 2: Rotate a list using list Comprehension..
    Method 3: Rotate a list using collections. deque. rotate().

    How do you shift a circular string in Python?

    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).