1
testText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nec mauris nec tellus mollis ullamcorper. Vestibulum sit amet arcu placerat, sagittis quam sed, rutrum sem. Morbi vulputate odio non lacus."
splitText = testText.split(" ")
print(splitText)
cleanedText = ''
for letter in testText:
    if letter in list('.,:;?!'):
        cleanedText.append(letter)
''.join(cleanedText)

I am trying to remove all punctuation in the paragraph above, but I am running into an "Attribute Error: 'str' object has no attribute 'append'".

What could potentially be going wrong and how should I go about resolving it?

Additionally, how would I then only print worlds equal to or longer than five characters and contain an 'i'?

2 Answers2

0

To remove a simple trick is to replace it with an empty str (with replace). For the second part we look at the 2 conditions: that i is in the word and and the length is equal to or greater than 5. Beware that we are looking at I in uppercase!

testText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nec mauris nec tellus mollis ullamcorper. Vestibulum sit amet arcu placerat, sagittis quam sed, rutrum sem. Morbi vulputate odio non lacus."
str_to_remove = list('.,:;?!')

for letter in str_to_remove:
    testText = testText.replace(letter, '')

for word in testText.split(' '):
    if 'i' in word and len(word) >= 5:
        print(word)
Lucas
  • 6,869
  • 5
  • 29
  • 44
0

try this:

for letter in testText:
    if letter not in list('.,:;?!\''):
        cleanText += letter
print(cleanText)
Dadep
  • 2,796
  • 5
  • 27
  • 40