- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I tell if a string repeats itself in Python?
We want to check whether the string we have is composed of repetitions of a substring of this string only. To check that we can check if a rotation of the string exists in a combination of 2 strings. This is because a string is periodic if and only if it is equal to a nontrivial rotation of itself.
The following code checks this and returns accordingly:
def find_period(s): # Concatenate 2 s and find s within # index one to end of the string i = (s+s).find(s, 1, -1) return None if i == -1 else s[:i] print find_period('012012012012012') print find_period('some random string')
This will give us the output:
- How can I tell if table row is in view using jQuery?
- How can I tell when a MySQL table was last updated?
- How can I test if a string starts with a capital letter using Python?
- How can I eliminate numbers in a string in Python?
- How can I convert bytes to a Python string?
- How can I convert a Python tuple to string?
- How can I fill out a Python string with spaces?
- How I can check if A is superclass of B in Python?
- How can I reverse a string in Java?
- How can I get last 4 characters of a string in Python?
- How can I create a python directory if it does not exist?
- How i can replace number with string using Python?
- How do I check if a string has alphabets or numbers in Python?