class Solution: def lengthOfLastWord(self, s): """ :type s: str :rtype: int """ l = [] for i in s[::-1].lstrip(): if i != ' ': l.append(i) else: break return len(l) w = Solution() print(w.lengthOfLastWord("hello world")) print(w.lengthOfLastWord("a "))