Search code examples
pythonpython-3.xfindall

Finding letters in a string


I'm trying to write a code that finds the letters in a string containing special characters, numbers and letters. The following code returns nothing:

a ="&*&)*&GOKJHOHGOUIGougyY^&*^x".lower()
print(a)
final = a.split()
for y in final:
    if (y.isalpha == True):
        print(y)

Output: &&)&gokjhohgouigougyy^&*^x => None

Can someone tell me what is the issue and how can I do it without using the re.findall, e.g. using loops like:

for(y in final):
    if (ord(y) in range (97, 127)):
        print(y)

The above code works:

for y in a:
    if (ord(y) in range (97, 127)):
        print(y, end='')

Solution

  • You need to call y.isalpha as y.isalpha() this is because isalpha is a function or method.

    >>> y='y'
    >>> y.isalpha
    <built-in method isalpha of str object at 0x00FA3A40>
    >>> y.isalpha()
    True
    

    Note that your split will give you words not letters - which may not be what you are expecting:

    >>> s = "Yes! These are words."
    >>> for w in s.split(' '):
    ...    print(w, w.isalpha())
    ...
    Yes! False
    These True
    are True
    words. False
    >>>
    

    One of the things to get used to in python is the difference between a property and a method - a property is something that you can read a method performs some action - dir lists both so for a string s you have:

    >>> dir(s)
    ['__add__', '__class__', '__contains__', '__delattr__', '__dir__',
     '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
     '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', 
     '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
     '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
     '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
     'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 
     'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum',
     'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 
     'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join',
     'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind',
     'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
     'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'
    ]
    

    Where:

    >>> s.__class__
    <class 'str'>
    

    is a property and:

    >>> s.capitalize
    <built-in method capitalize of str object at 0x03529F50>
    

    is a method and needs to be called by the addition of parenthesis () to actually perform their function. It is worth also distinguishing between methods that return a value and those that operate in place.

    >>> s.lower()
    'yes! these are words.'
    

    Returns a value as does s.split() but sort is an in-place operation, e.g.:

    >>> words = s.lower().split()
    >>> words
    ['yes!', 'these', 'are', 'words.']
    >>> words.sort()
    >>> words
    ['are', 'these', 'words.', 'yes!']
    >>>