您的函数可以简单如下:def get_word_containing(string, char): words = [word for word in string.split() if char in word] return wordsstring = "I only want the $1000"print(get_word_containing(string, "$"))输出:['$1000']
我将稍微修改 @Biplob 函数来打印字符串:def get_word_containing(myStr, char): for x in myStr.split(): if char in x: print(x)mystring = "I only want the $1000"get_word_containing(mystring, "$")