Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding duplicate words in a string python

I am trying to make my function locate duplicate words and if so the output should be True or False depending on wether there are duplicate words. For example:

doubleWord("cat") --> False .      
doubleWord("catcat") --> True .   
doubleWord("contour" * 2) --> True

So far I have this:

def main():

    word = input("Enter a string: ")

    half = len(word) >> 1
    if word[:half] == word[half:]:
        print("True")
    else:
        print("False")

    return
    print(main())

if name == "main": main()

Any help would be greatly appreciated. I thought maybe using slicing would make it easier but I have no idea how to implement that in my code. Thanks!

like image 497
Jane Doe2 Avatar asked Nov 27 '25 17:11

Jane Doe2


1 Answers

You just have to compare the first part with the second, you can do this with slicing like this:

def doubleWord(word):
    return word[len(word) // 2:] == word[:len(word) // 2]
like image 175
Francisco Avatar answered Nov 30 '25 05:11

Francisco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!