Program to get the length of the last word in a string using a function

In this python function program, we will get the length of the last word in a string using a function. Length means total number of characters/integers present in the word/number.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function length.
  2. Use the def keyword to define the function.
  3. Take a string as input through the user.
  4. Convert the string into a list by splitting the string using split(” “).
  5. Get the length of the last word using the indexing and len() function.
  6. Print the output.
  7. Call the function to get the output.
				
					def length():
    str1 = input("Enter string: ")
    l = str1.split(" ")
    print(f"Length of the last word {l[-1]} in the string: ",len(l[-1]))
length()
				
			

Output :

				
					Enter string: sqatools in best for learning python
Length of the last word python in the string:  6
				
			

Related Articles

search words in a string.

get a valid mobile number.

Leave a Comment