String Rotation

Check whether one string is a rotation of another.

IntermediateTopic: String Programs
Back

Python String Rotation Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to check if one string is a rotation of another

s1 = input("Enter first string: ")
s2 = input("Enter second string: ")

if len(s1) != len(s2):
    print("Not rotations (different lengths).")
else:
    if s2 in (s1 + s1):
        print("Strings are rotations of each other.")
    else:
        print("Strings are not rotations of each other.")
Output
Enter first string: ABCD
Enter second string: CDAB
Strings are rotations of each other.

Understanding String Rotation

If s2 is a rotation of s1, it must appear as a substring inside s1 + s1.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents