lengthOfLastWord

来源:互联网 发布:网络什么最赚钱的方法 编辑:程序博客网 时间:2024/06/01 07:53

输出字符串中最后一个单词的长度

if not s:    print(0)else:    i = 1    n = 0    while i <= len(s) and s[-i] == ' ':        i = i + 1    while i <= len(s) and not (s[-i] ==' '):        i = i + 1        n = n + 1print(n)