【发布时间】:2016-11-18 12:26:50
【问题描述】:
寻找关于如何实现以下说明的更好说明。
# loop while i is less than the length of name and the i-th character is not a space. # return the part of name up to but not including the i-th character to the caller.
def get_first_name(name):
i = 0
while i < len(name) and '' in str(i):
i += 1
return name
【问题讨论】:
-
“第i个字符”是索引
i处的字符,即name[i]。请注意,通常不需要按索引迭代 Python 字符串 - 您可以改为使用for char in name:来获取每个字符。