Search code examples
pythonstringalphanumeric

I would like to print all alphabet letters in the string below, however, I do not get an output when I run the code. What am I doing wrong?


I would like to print all alphabet letters in the string below, however, I do not get an output when I run the code. Not even an error. What am I doing wrong ?

        x='a14b8c789d45e17'
        for i in x:
            if i== '%s' %x:
        print(i)

#No output


Solution

  • You're currently testing whether a single character equals the entire string. Instead, test each substring with the str.isalpha() method to test if they're letters or not:

    x = 'a14b8c789d45e17'
    for i in x:
        if i.isalpha():
           print(i)