Determine if a string only contains repetitions of a substring This is my first Python program in years, it's just a simple string manipulation program. Given a string, determine if it contains repetitions of any substring. For example, testtest contains two substring test and nothing else.

def onlySubstrings(stringOrig):
    stringCopy = (stringOrig+'.')[:-1] # copy string contents
    for i in range(1, len(stringCopy)): # for each character
        stringCopy = stringCopy[1:]+stringCopy[0] # shift first char to end
        if stringCopy == stringOrig: # if equivalent to original input 
            return True # it consists of substrings only
    else: # otherwise the only substring is the string itself
        return False

if __name__ == '__main__':
    wordList = ['testtest','testteste','test','tetestst'] # some test words
    print('The below words only contain substring repetitions:')
    for word in wordList: # for each word
print(onlySubstrings(word), '\t', word)


If you imagine the string as circular unto itself, the string can only contain equal substring(s) if you can move the characters to the left or right and get the same original string in less than len shifts:

testtest - 0 shifts, original string
esttestt - 1 shift, not original
sttestte - 2 shifts, not original
ttesttes - 3 shifts, not original
testtest - 4 shifts, original again


Since 4 &lt; len, it contains only substring repetitions.

testteste - 0 shifts, original string
esttestet - 1 shift, not original
...
etesttest - (len-1) shifts, not original
testteste - len shifts, original again


Since we couldn't find the original string again in under len shifts, it does not only contain repetitions of a substring.
The code is relatively concise and easy to read. Here are some points that could be improved:


A general thing is the amount of commenting, you should generally resort to comments only when it's hard to make the code self-explanatory. You don't really need to comment each line of the code.
stringCopy = (stringOrig+'.')[:-1] # copy string contents
You can make a copy this way instead stringCopy = stringOrig[:].
To make the code more readable, you can create a function that shifts a string n characters. This will really help make the code more readable.
The else keyword after the for loop is not needed since you return anyway if you didn't finish the loop. else is usually used with break in the for loop.
The name of the function could be slightly improved, maybe consists_of_repeated_substrings. This is longer but is very easy to understand.
A note on performance: your algorithm creates a copy of the original string in each iteration. This works fine for shorter strings, but if you wanna make it more efficient you can consider matching the string without making a shifted copy of it.


Incorporating these comments you could rewrite the code as follows:

def shifted_string(str, n):
  return str[n:] + str[:n]

def consists_of_repeated_substrings(str):
    '''
    Checks whether a string consists of a substring repeated multiple times.
    Ex. 'testtesttest' is the string 'test' repeated 3 times.
    '''

    # The string can only contain equal substrings if you can shift it n 
    # times (0 &lt; n &lt; len) and get the same original string.
    for i in range(1, len(str)):
        if shifted_string(str, i) == str:
            return True
    return False




A clever algorithm for doing the same thing is as follows:

double_str = (str + str)[1:-1]
return double_str.find(str) != -1


The idea is similar to yours, so I will leave understanding it as an exercise.