问题出在空格的处理上,函数只能适用于以空格分隔的单词形式。可以使用split()函数来解决这个问题。具体步骤如下:
示例代码如下所示:
def print_second_word(sentence): words = sentence.split() if len(words) >= 2: return words[1] else: return ""
print(print_second_word("I am a student")) # 输出 am print(print_second_word("ghanou djellal")) # 输出 djellal print(print_second_word("hello")) # 输出 ""