题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
string = input() length = 0 for i in string[::-1]: if i != ' ': length += 1 else: break print(length)上面是将字符串倒过来循环,碰到空格结束循环。没有用字符串的split的考虑是想节约内存,如果用split分割字符串的话写起来确实简洁方便,不过会多一个list的空间,两个都贴上。
string = input() print(len(string.split(' ')[-1]))