AttributeError: 'builtin_function_or_method' object has no attribute 'replace'

1089 picture 1089 · Jul 12, 2014 · Viewed 35.1k times · Source

When I try to use this in my program, it says that there's an attribute error

'builtin_function_or_method' object has no attribute 'replace'

but I don't understand why.

def verify_anagrams(first, second):
    first=first.lower
    second=second.lower
    first=first.replace(' ','')
    second=second.replace(' ','')
    a='abcdefghijklmnopqrstuvwxyz'
    b=len(first)
    e=0
    for i in a:
        c=first.count(i)
        d=second.count(i)
        if c==d:
            e+=1
    return b==e

Answer

user2555451 picture user2555451 · Jul 12, 2014

You need to call the str.lower method by placing () after it:

first=first.lower()
second=second.lower()

Otherwise, first and second will be assigned to the function object itself:

>>> first = "ABCDE"
>>> first = first.lower
>>> first
<built-in method lower of str object at 0x01C765A0>
>>>
>>> first = "ABCDE"
>>> first = first.lower()
>>> first
'abcde'
>>>