【发布时间】:2022-01-04 20:41:22
【问题描述】:
我的任务是编写一个带有一个返回字符串的字符串参数的函数。该函数应提取此字符串中的单词,删除空单词以及等于“end”和“exit”的单词,将 其余单词为大写,用连接标记字符串“;”将它们连接起来并返回这个 新加入的字符串。
这是我的函数,但如果字符串不包含单词“exit”或“end”,则不会返回新字符串:
def fun(long_string):
stop_words = ('end', 'exit', ' ')
new_line = ''
for word in long_string:
if word in stop_words:
new_line = long_string.replace(stop_words, " ")
result = ';'.join(new_line.upper())
return result
print(fun("this is a long string"))
【问题讨论】:
-
你能举一个期望输出的例子吗?
标签: python string function replace