def first_word(text):
index = text.find(" ")
return text[:index] if index != -1 else text
first_word("Hello world") == "Hello"
first_word("a word") == "a"
first_word("hi") == "hi"
text.find(" ")功能被遗忘,导致使用了弯弯绕绕才做出来
原始代码:
a="a word"
d=[]
for i in a:
if i==" ":
break
else:
d+=i
f="".join(str(i) for i in d)
f
发现更简单的了
a="a word"
c=a.split()
c[0]