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:
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 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.
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://w3resource.com/python-exercises/challenges/1/python-challenges-1-exercise-30.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics