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
- Create a function length.
- Use the def keyword to define the function.
- Take a string as input through the user.
- Convert the string into a list by splitting the string using split(” “).
- Get the length of the last word using the indexing and len() function.
- Print the output.
- 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
Python function program to get a valid mobile number.
Python function program to convert an integer to its word format.
Python function program to get all permutations from a string.
Python function program to add two numbers.
Python function program to print the input string 10 times.
Python function program to print a table of a given number.