w3resource logo
Python Exercises

Python Challenges: Find the length of the last word

Python Challenges - 1: Exercise-30 with Solution

Write a Python program to find the length of the last word.

Explanation:

Python: Find the length of the last word

Sample Solution:-

Python Code:

def length_of_last_word(s):
        words = s.split()
        if len(words) == 0:
            return 0
        return len(words[-1])

print(length_of_last_word("Python Exercises"))
print(length_of_last_word("Python"))
print(length_of_last_word(""))
print(length_of_last_word("  "))

Sample Output:

9                                                                       
6                                                                       
0                                                                       
0 

Flowchart:

Python Flowchart: Find the length of the last word

Python Code Editor:

Improve this sample solution and post your code through Disqus