Python - Check if string repeats itself Last Updated : 15 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Checking if a string repeats itself means determining whether the string consists of multiple repetitions of a smaller substring. Using slicing and multiplicationWe can check if the string is a repetition of a substring by slicing and reconstructing the string using multiplication. Python s = "ababab" # Check if string repeats by slicing and multiplying res = s in (s + s)[1:-1] print(res) OutputTrue Explanation:We concatenate the string with itself to allow rotation-based checks.We remove the first and last characters of the concatenated string.If the original string exists in this modified string, it means it is made up of repeated substrings.Let's explore some more methods and see how we can check if string repeats itself in python.Table of ContentUsing regular expressionsUsing for loopUsing concatenationUsing string slicing and comparisonUsing regular expressionsWe can use a regular expression to check for a repeating pattern. Python import re s = "ababab" # Check for repeating pattern using regex res = bool(re.fullmatch(r"(.+)\1+", s)) print(res) OutputTrue Explanation:The regular expression (.+)\1+ checks for one or more occurrences of a substring followed by itself.If the string matches this pattern, it repeats itself.Using for loopWe can manually iterate through the string using a for loop to find the smallest substring that repeats to form the entire string. Python s = "ababab" # Initialize result as False res = False # Iterate through possible lengths of substrings for i in range(1, len(s) // 2 + 1): # Check if current length divides the string evenly if len(s) % i == 0: # Check if the substring repeats to form the string if s[:i] * (len(s) // i) == s: res = True break print(res) OutputTrue Explanation:We iterate through all possible lengths of substrings up to half the length of the string.If the substring repeats to match the entire string, we set the result to True.Using concatenationAnother simple method involves using find() function after concatenating the string with itself. Python s = "ababab" # Check for repeating pattern using string concatenation res = (s + s).find(s, 1) != len(s) print(res) OutputTrue Explanation:We concatenate the string with itself and check if the original string appears again, starting from the second character.If it does, the string is made of repeated substrings.Using string slicing and comparisonWe can use slicing and comparison to check for a repeating pattern by dividing the string into smaller parts. Python s = "ababab" # Assume no repetition initially res = False # Check for repeating pattern by slicing for i in range(1, len(s)): if len(s) % i == 0 and s[:i] * (len(s) // i) == s: res = True break print(res) OutputTrue Explanation:We divide the string into smaller slices and check if they form the entire string through repetition. Comment More infoAdvertise with us Next Article Check if String is Empty or Not - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python - Check for spaces in string Sometimes, while working with strings in Python, we need to determine if a string contains any spaces. This is a simple problem where we need to check for the presence of space characters within the string. Let's discuss different methods to solve this problem.Using 'in' operator'in' operator is one 3 min read How to Repeat a String in Python? Repeating a string is a simple task in Python. We can create multiple copies of a string by using built-in features. This is useful when we need to repeat a word, phrase, or any other string a specific number of times. Using Multiplication Operator (*):Using Multiplication operator (*) is the simple 2 min read Check if String is Empty or Not - Python We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example:Using Comparison Operator(==)The si 2 min read Shrink List for Repeating Elements - Python We are given a list of elements that may contain repeated values. Our task is to shrink the list by ensuring that each repeated element appears only once while maintaining the order of their first occurrence. For example, if the input list is [2, 3, 2, 5, 3, 5], the output should be [2, 3, 5].Using 3 min read How to Count Repeated Words in a String in Python In this article, we will learn how to count repeated words in a string. Python provides several methods to Count Repeated Words , such as dictionaries, collections. Counter module, or even regular expressions. The simplest way to count repeated words is by splitting the string into individual words 2 min read Check if string contains character - Python We are given a string and our task is to check if it contains a specific character, this can happen when validating input or searching for a pattern. For example, if we check whether 'e' is in the string 'hello', the output will be True.Using in Operatorin operator is the easiest way to check if a c 2 min read Like