Problem statement:
In String Rotation in Python, we are given two strings, we need to find the number of rotations required to match 1 string to another string. If there is no match, print No rotations found.
Code for String Rotation in Python:
# importing deque from collections module
from collections import deque
# declaring 2 string
mystr1 = 'copyassignment'
mystr2 = 'assignmentcopy'
# declaring an empty list
allrotations = []
# finding all rotations of mystr1 and storing them to allrotations
for i in mystr1:
items = deque(mystr1)
items.rotate(1)
mystr1 = list(items)
mystr1 = ''.join(mystr1)
allrotations.append(mystr1)
found = False
# checking the mystr2 in allrotations and printing number of rotations required
for i in range(len(allrotations)):
if mystr2==allrotations[i]:
found = True
print(i+1)
else:
if not found:
print("No rotations found")
Output:

Explanation:
We will understand the logic behind the code, not the code. So, we have used deque from the collections library to find rotations of a string. Then, we have generated all rotations inside a for loop and stored them in allrotations list. Then, have compared all the rotations with the second string and found the index number of the rotated string which matches and prints its index number. The index number is equal to the number of rotations required to rotate the first string to make it equal to the second string.
Also Read:
- Hyphenate Letters in Python
- Earthquake in Python | Easy Calculation
- Striped Rectangle in Python
- Perpendicular Words in Python
- Composite Number in Python
- Greatest Among Four Numbers in Python
- Reverse the sentence in Python
- Denominations in Python
- Min and max values in an array in JavaScript
- Keyboard events in JavaScript
- Reaching Ground Floor in Python
- Number of Moves in Python
- Starks Adventure in Python
- Neutralization in Python | Assignment Expert
- Free shipping in Python
- Raj has ordered two electronic items Python | Assignment Expert
- First Place in Python
- Triples with Properties in Python
- Nested list Indexing Python
- Team Points in Python
- Two words combination in Python
- ID Card in Python
- Cipher with a key in Python | Assignment Expert
- Multiple of 5 in Python
- Sandglass Star in Python
- Multiple of 3 in Python | Assignment Expert
- Ticket selling in Cricket Stadium using Python | Assignment Expert
- Sum of List Elements in Python
- All possible subsets in Python
- Names and Nicknames in Python