Python | Check if string repeats itself
While working with strings, many times, we can come across a use case in which we need to find if a string has in it the repeating substring, which repeats all over the string and thus making a multiple of the root substring. Let’s discuss certain ways in which we can get the root substring of string.
Method #1 : Using List comprehension + Brute Force
We can perform this task using selective slicing and brute force manner. This is the naive method to find the string in which we try to get the root string by repetitive division of string.
# Python3 code to demonstrate working of # Check if string repeats itself # Using List comprehension + Brute Force # initializing string test_str = "GeeksforGeeksGeeksforGeeksGeeksforGeeks" # printing original string print("The original string is : " + test_str) # using List comprehension + Brute Force # Check if string repeats itself res = Nonefor i in range(1, len(test_str)//2 + 1): if (not len(test_str) % len(test_str[0:i]) and test_str[0:i] * (len(test_str)//len(test_str[0:i])) == test_str): res = test_str[0:i] # printing result print("The root substring of string : " + res) |
The original string is : GeeksforGeeksGeeksforGeeksGeeksforGeeks The root substring of string : GeeksforGeeks
Method #2 : Using list slicing + find()
This problem can also be solved using the fact that we can search for root string after adding a string and checking the root string in this string except last and first character, represents the string is repeating itself.
Doesn’t work for string length < 2.
# Python3 code to demonstrate working of # Check if string repeats itself # Using list slicing + find() # initializing string test_str = "GeeksforGeeksGeeksforGeeksGeeksforGeeks" # printing original string print("The original string is : " + test_str) # using list slicing + find() # Check if string repeats itself res = Nonetemp = (test_str + test_str).find(test_str, 1, -1) if temp != -1: res = test_str[:temp] # printing result print("The root substring of string : " + res) |
The original string is : GeeksforGeeksGeeksforGeeksGeeksforGeeks The root substring of string : GeeksforGeeks
Recommended Posts:
- Python | Check if given string can be formed by concatenating string elements of list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if string ends with any string in given list
- Python | Check if a given string is binary string or not
- Python | Check for URL in a String
- Python | Check whether string contains only numbers or not
- Python | Check for spaces in string
- Python | Check for float string
- Python set to check if string is panagram
- Python Set | Check whether a given string is Heterogram or not
- Python | Check for ASCII string
- Python | Check if a variable is string
- Python | Check if given string is numeric or not
- Check if both halves of the string have same set of characters in Python
- Python program to check if given string is pangram
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



