Check String is Rotated in Python

  • Post category:String

You can use the following code to check string is rotated. A rotation is a circular shift of a string. For example, if you rotate the string “CodeAllow” by two characters, you get “deAllowCo”. To check if a string is a rotation of another string, you can concatenate the string with itself and check if the other string is a substring of the concatenated string.

Step 1: Create two strings

str1 = "CodeAllow"
str2 = "AllowCode"

print("str1 = ", str1)
print("str1 type = ", type(str1))

print("str2 = ", str2)
print("str2 type = ", type(str2))

Step 2: Check if the string is rotated or not

if len(str1) == len(str2) and str2 in str1 + str1:

    print("Not Rotation")  


str1 =  CodeAllow
str1 type =  <class 'str'>
str2 =  AllowCode
str2 type =  <class 'str'>

Free resources to learn advanced skills: AiHints and CodeAllow