How would I be able to make this if statement more efficient?
if ',' in my_string:
my_string = my_string.split(',', 1)[0]
return ', '
elif '.' in my_string:
my_string = my_string.split('.', 1)[0]
return '. '
elif '?' in my_string:
my_string = my_string.split('?', 1)[0]
return '? '
elif '!' in my_string:
my_string = my_string.split('!', 1)[0]
return '! '
elif ';' in my_string:
my_string = my_string.split(';', 1)[0]
return '; '
else:
self.qstring = my_string
return None
I could make a list like:
my_list = [',', '.', '?', '!', ';']
and loop through, but I'm not sure how to still use the else statement if I put it all in a loop. Any thoughts?
my_stringis preserved somewhere? As your code stands, there is no point in assigning tomy_string, or even splitting the text, as you return immediately after the split and are thus discardingmy_string.