How can I tell if a string repeats itself in Python?

| | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

I"m looking for a way to test whether or not a given string repeats itself for the entire string or not.

Examples:

[
    "0045662100456621004566210045662100456621",             # "00456621"
    "0072992700729927007299270072992700729927",             # "00729927"
    "001443001443001443001443001443001443001443",           # "001443"
    "037037037037037037037037037037037037037037037",        # "037"
    "047619047619047619047619047619047619047619",           # "047619"
    "002457002457002457002457002457002457002457",           # "002457"
    "001221001221001221001221001221001221001221",           # "001221"
    "001230012300123001230012300123001230012300123",        # "00123"
    "0013947001394700139470013947001394700139470013947",    # "0013947"
    "001001001001001001001001001001001001001001001001001",  # "001"
    "001406469760900140646976090014064697609",              # "0014064697609"
]

are strings which repeat themselves, and

[
    "004608294930875576036866359447",
    "00469483568075117370892018779342723",
    "004739336492890995260663507109",
    "001508295625942684766214177978883861236802413273",
    "007518796992481203",
    "0071942446043165467625899280575539568345323741",
    "0434782608695652173913",
    "0344827586206896551724137931",
    "002481389578163771712158808933",
    "002932551319648093841642228739",
    "0035587188612099644128113879",
    "003484320557491289198606271777",
    "00115074798619102416570771",
]

are examples of ones that do not.

The repeating sections of the strings I"m given can be quite long, and the strings themselves can be 500 or more characters, so looping through each character trying to build a pattern then checking the pattern vs the rest of the string seems awful slow. Multiply that by potentially hundreds of strings and I can"t see any intuitive solution.

I"ve looked into regexes a bit and they seem good for when you know what you"re looking for, or at least the length of the pattern you"re looking for. Unfortunately, I know neither.

How can I tell if a string is repeating itself and if it is, what the shortest repeating subsequence is?

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from How can I tell if a string repeats itself in Python?, check other ast Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Ken Chamberlet

Munchen | 2023-03-22

Strings PHP module is always a bit confusing 😭 How can I tell if a string repeats itself in Python? is not the only problem I encountered. Checked yesterday, it works!

Davies Robinson

Boston | 2023-03-22

Thanks for explaining! I was stuck with How can I tell if a string repeats itself in Python? for some hours, finally got it done 🤗. Checked yesterday, it works!

Boris Wu

Boston | 2023-03-22

I was preparing for my coding interview, thanks for clarifying this - How can I tell if a string repeats itself in Python? in Python is not the simplest one. I just hope that will not emerge anymore

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically