-1

I am having trouble getting my program to detect non-alphabetical characters and am stuck. Earlier I tried to use the re.search function, (ie re.search('[a-z]') . I currently do not understand this function and opted for code I can currently understand. If someone could explain the regex function and help me with my code it I would appreciate it.

Here is my code:

# Eliminates characters that are not part of the alphabet from string
def eleminate_unknown(input_string):
    alpabet_list= ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')

    #checks if string is upper case and makes it lower case
    if (input_string != input_string.lower()):
        print 'Upper case letters detected. Converting string to lower case.'
        input_string = input_string.lower()
    print("eleminating unknown charecters from %s") %input_string

    #creates visable division
    print '___________________________________________________________'

    for count in range(-1,(len(input_string)-1)): #need to subtract one from len of string to match index
        print input_string

        #looks for and eliminates characters not in lower case alphabet

        if (input_string[count] not in alpabet_list == False):
            print "String index %s eleminated!" %count
            print "The charecter %s died a terrable death" %input_string[count]
            print input_string[:count] + input_string[count+1:]
            input_string = input_string[:count] + input_string[count+1:]
            count = count+1
        else:
            count = count+1
            print "Index %s is not eliminated. Character %s is recognized and lives to die another day." % (count, input_string[count])
    print input_string
Stephen Rauch
  • 40,722
  • 30
  • 82
  • 105

2 Answers2

0

Not the most elegant solution, but it works:

from string import ascii_lowercase
from string import ascii_uppercase

def eliminate_letters(in_string):
    for c in ascii_lowercase:
        without_lowercase = in_string.replace(c, '')
    for C in ascii_uppercase:
        no_letters = without_lowercase.replace(C, '')
    print(no_letters)
    return no_letters

Explanation: ascii_lowercase is a string of all the lowercase letters ('abcdefghijklmnopqrstuvwxyz') and ascii_uppercase is a string of all the CAPITAL LETTERS. The replace() function can be found here, as can most things you're looking for in Python. Each character in the collection of letters gets replaced by the statements in the for loops, and then the letter-less string gets returns by the eliminate_letters() function

Nathan
  • 646
  • 1
  • 8
  • 25
0

Looks like you are complicating the process. range(n) keyword iterates until the n-1. For example range(5) iterates from 0 to 4.So it is enough to use range(len(input_string)). Moreover in your code in addition to for loop iteration, you are iterating again using count = count+1. So program skips one letter for every iteration.

alpabet_list=('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')

#checks if string is upper case and makes it lower case
if (input_string != input_string.lower()):
    print 'Upper case letters detected. Converting string to lower case.'
    input_string = input_string.lower()
print("eleminating unknown charecters from %s") %input_string

#creates visable division
print '___________________________________________________________'
output_string=""
for letter in input_string: 
    print input_string

    if (letter not in alphabet_list):
        print ("The character" +letter+ "died a terrable death")
    else:
        output_string = output_string+letter
    print output_string

print output_string