I have been asked to write a function which returns a string with all the specified punctuations at the beginning and end of the each word removed without affecting those which are within words like ban,ana. How could I do this using loops?
def beautify_sentence(sentence, punctuation):
for element in punctuation:
sentence = sentence.replace(element, "")
return sentence
print(beautify_sentence("?hello !mango! ...and., ban,ana.. yum?? apple!", "!?.,"))
# correct answer = hello mango and ban,ana yum apple
# my answer = hello mango and banana yum apple
This is where you can use str.strip
on your individual words:
def beautify_sentence(sentence, punctuation):
return ' '.join([x.strip(punctuation) for x in sentence.split()])
>>> beautify_sentence("?hello !mango! ...and., ban,ana.. yum?? apple!", "!?.,")
hello mango and ban,ana yum apple
Worth mentioning that python has strip, rstrip, and lstrip. lstrip and rstrip remove characters for the start and end of the line.
Using the similar code that you have.
sentance="?hello !mango! ...and., ban,ana.. yum?? apple!"
punctuations="!?.,"
def beautify_sentence(sentence, punctuation):
words = []
for word in sentance.split(' '):
words.append(word.lstrip(punctuations).rstrip(punctuations))
return " ".join(words)
print(beautify_sentence(sentance, punctuations))
But as already mentioned strip will removed front and back.
sentance="?hello !mango! ...and., ban,ana.. yum?? apple!"
punctuations="!?.,"
def beautify_sentence(sentence, punctuation):
words = []
for word in sentance.split(' '):
words.append(word.strip(punctuations))
return " ".join(words)
print(beautify_sentence(sentance, punctuations))
I think this is a learning task. So, I am sharing another approach to solve this problem.
Alternative Approach
We can iterate the sentence and remove any listed punctuation which are not middle of any word.
def beautify_sentence(sentence, punctuation):
beautiful_sentence = ""
for i in range(len(sentence)):
if sentence[i] in punctuation:
# remove punctuation at start and end of a sentence
if i == 0 or i == len(sentence)-1:
continue
# remove punctuation which are not in middle of a word
if not sentence[i-1].isalpha() or not sentence[i+1].isalpha():
continue
beautiful_sentence += sentence[i]
return beautiful_sentence
if __name__ == "__main__":
test_sentence = "?hello !mango! ...and., ban,ana.. yum?? apple!"
test_punctuations = "!?.,"
expected_sentence = "hello mango and ban,ana yum apple"
sentence = beautify_sentence(test_sentence, test_punctuations)
assert sentence == expected_sentence, (expected_sentence, sentence)
This is not tested for all test cases though. You may need to modify it to meet all criterias.
A little bit longer, but I hope you can easily understand the algorithm from this step by step approach.
def beautify(sentence, punctuation):
sen = sentence
punc = punctuation
words = sen.split(" ") # splits into a list of words
ret = [] # return the processed words
for word in words: # process each word one by one
noPuncInTheStart = False
noPuncInTheEnd = False
# don't really mind this. See the break statements!
while (True):
# Check the length of the word to prevent error
# And if there is any punctuation in the beginning
if (len(word) > 0 and not noPuncInTheStart):
if (word[0] in punc): # if the word started by punc
word = word[1:] # remove the first character
else: # there is no any punctuation anymore
noPuncInTheStart = True
else:
# the word is no longger exists :)
# in case the word is all punctuation, no letters, etc
break
if (len(word) > 0 and not noPuncInTheEnd):
if (word[-1] in punc): # check if the end is a punc
word = word[:-1] # remove the end of the character
else:
noPuncInTheEnd = True
else:
break
if (noPuncInTheStart and noPuncInTheEnd):
# there's no longger punc, neither in the beginning
# nor the end
break
ret.append(word) # add the processed word to the `ret` list
ret = " ".join(ret) # join the `ret` list into sentence
return ret
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.