Check String Rotation

Check if one string is rotation of another.

AdvancedPhase 5 - Category 5: Advanced String LogicExample 10 of 14
check-string-rotation.py
1# Take two strings
2s1 = input("Enter first string: ")
3s2 = input("Enter second string: ")
4
5# Check rotation
6if len(s1) == len(s2):
7 # Concatenate s1 with itself
8 temp = s1 + s1
9 if s2 in temp:
10 print("Rotation")
11 else:
12 print("Not a rotation")
13else:
14 print("Not a rotation (different lengths)")

Output

Enter first string: hello
Enter second string: lohel
Rotation

What's going on

Concatenate string with itself and check substring.

Key Concepts:

Check if lengths equal
Concatenate s1 with itself
Check if s2 is substring