Watson Cole Watson Cole -4 years ago 107
Python Question

IndexError: string index out of range for if statments

I'm trying to make it so that my program will break up a sentence into a list

alpha =["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"]
def PARSE(inpu): #Parse and remove symbols
PARSE = list()
d = 0

intv = 0
output = 0
for i in range (len(inpu)):
intv = intv + 1
if inpu[intv] in alpha:
output = output, inpu[intv]
intv = 0
LI1 = 1
LI2 = 1
while len[output] != LI1 - 1:
PARSE.append("")
while not len[output] - 1 < LI1 or not output[LI1] == '':
PARSE[LI2 - 1] = PARSE[LI2] + output[LI1]
LI1 = LI1 + 1
last = last + 1
LI1 = LI1 + 1
LI2 = LI2 + 1
PARSE[last] = PARSE[last] + output[LI1 - 1]
done = 1


But I get this error mesage

IndexError: string index out of range


on

if inpu[intv] in alpha:


How do I fix this?

Answer Source

If you are trying to filter out all punctuation and symbols, try this:

import string

def Parse(inpu)

   return ''.join(i for i in inpu if i.lower() in string.ascii_lowercase)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download