iterate through a string and edit to reform string

Viewed 16

Hi so I have a bunch of sentences and I want to clean them up and reform the sentences

I have this so far but I know there's a ways to go. Its been a while since I took python and I know its a loop issue but I'm stuck on next steps. Any tips are much appreciated!

r='hello. with us today is >John Smith from c-n-n.'
n=r.split()

for i in n:
    if '>' in i:
        x=i.replace('>','')
        print(x)
    if '.' in i:
        x=i.replace('.','')
        if '-' in x:
            i=x.replace('-','').upper()
            print(i)
        else:print(x)

I want the end result to be : hello with us today is John Smith from CNN

1 Answers

I don't really get the rules you're trying to apply. The following code might help, or just do what you require.

r: str = 'hello. with us today is >John Smith from c-n-n.'
new_words: list[str] = []
for word in r.split():
    new_word: str = word
    if '>' in new_word:
        new_word = new_word.replace('>', '')
    if '.' in new_word:
        new_word = new_word.replace('.', '')
    if '-' in new_word:
        new_word = new_word.replace('-', '').upper()
    new_words.append(new_word)
print(' '.join(new_words))
Related