w3resource

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:

Contribute your code and comments through Disqus.

Previous: Write a Python program to find majority element in a list.
Next: Write a Python program to add two binary numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/challenges/1/python-challenges-1-exercise-30.php