In the previous article, we have discussed Python Program to Subtract two Complex Numbers
Given a string that contains the words the task is to print the last word in the given string in Python.
Examples:
Example1:
Input:
Given string =hello this is BTechgeeks
Output:
The last word in the given string { hello this is BTechgeeks } is: BTechgeeks
Example2:
Input:
Given string =good morning this is btechgeeks
Output:
The last word in the given string { good morning this is btechgeeks } is: btechgeeks
- Python Program to Remove the last Word from the String/Sentence
- Python Program to Remove a String from a List of Strings
- Python Program to Sort words in Alphabetic Order
Program to get the Last Word from a String in Python
Below are the ways to get the last word from the given string in Python.
Method #1: Using split() Method (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Split the words of the given string to a list of words using the built-in split() function.
- Get the last word from the above list of words using negative indexing and store it in a variable.
- Print the last word of the given string by printing the above variable.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvnstrng = 'hello this is BTechgeeks' # Split the words of the given string to a list of words # using the built-in split() function. lsttofwrds = gvnstrng.split() # Get the last word from the above list of words using negative indexing # and store it in a variable. lstwrd = lsttofwrds[-1] # Print the last word of the given string by printing the above variable. print('The last word in the given string {', gvnstrng, '} is:', lstwrd)
Output:
The last word in the given string { hello this is BTechgeeks } is: BTechgeeks
Method #2: Using split() Method (User Input)
Approach:
- Give the string as user input using the input() function and store it in the variable.
- Split the words of the given string to a list of words using the built-in split() function.
- Get the last word from the above list of words using negative indexing and store it in a variable.
- Print the last word of the given string by printing the above variable.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using the input() function and store it in the variable. gvnstrng = input('Enter some random string = ') # Split the words of the given string to a list of words # using the built-in split() function. lsttofwrds = gvnstrng.split() # Get the last word from the above list of words using negative indexing # and store it in a variable. lstwrd = lsttofwrds[-1] # Print the last word of the given string by printing the above variable. print('The last word in the given string {', gvnstrng, '} is:', lstwrd)
Output:
Enter some random string = good morning this is btechgeeks The last word in the given string { good morning this is btechgeeks } is: btechgeeks
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
- Python Program to Remove the First Occurrence of Character in a String
- Program to Print Even Length Words in a String
- Python Program to Print Nth word in a given String
- Python Program to Remove all Consonants from a String