How to Print the Last Word in a sentence using Python?


Introduction

This article will be focusing mainly on how to print the last word in a sentence using Python.

We will be using a simple technique to achieve the task. Python is an open-source, flexible, powerful programming language that provides us with various modules and functionalities that helps us to manipulate strings easily.

For printing the last word in a sentence, we will use Python’s built-in string functions. Our approach is that we will first break down the input sentence i.e., given input string into a list of words and access the last element of the list to obtain the desired result. This approach is very much useful in solving this type of problem as we do not have to worry about the length of the sentence which makes it a very useful technique.

Printing the last word in a sentence is used in a text-processing project, Data extraction, and data analysis. It is also used in text editing and parsing.

So, let us now learn the function used in the program, the approach of the code with examples, and the use case.

Syntax

Let us now look at the syntax of the various functions that we will be using in the article.

split(separator, maxsplit)

The split() method splits a string into a list.

Separator − It is optional, it specifies the separator to use when splitting the string. By default, any whitespace is a separator.

maxsplit − It is optional and it specifies how many splits to do. The default value is -1, which is "all occurrences".

Approach

Let us now look into the approach of the program. We are going to take a string as input and then we are going to split it into a list of words and we will extract the last word. So now learn the step-by-step breakdown of the code.

  • First we are defining a function named print_last_word which will take a string as an input and store it into a variable "sentence".

  • After that we will split the string into a list of strings using the split() function. This function splits the string at every whitespace character and stores it in the form of a list.

  • Then we will extract the last index of the string and store it in the "last_word" variable.

  • In the end we will print the result on the console screen.

  • After printing the result program terminates.

With this approach, we can easily retrieve the last word of the string without worrying about the length and print that word on the console screen.

Example

The code snippet for the above problem is −

def print_last_word(sentence):
   # Split the sentence into a list of words
   words = sentence.split()
 
   # Retrieve the last word from the list
   last_word = words[-1]
 
   # Print the last word
   print(last_word)
 
   # Example usage
   sentence = input("Write a sentence ")
   print_last_word(sentence)

Let us now understand the concept with an example where the input sentence is "Tutorial point is a good place for learning"

  • The program starts and displays a message "Write a sentence" on the console window asking for the user's input.

  • The text entered by the user is "Tutorial point is a good place for learning"

  • Then the program calls the function "print_last_word" which passes the user input string as an argument to the function.

  • The string is then stored in the variable whose name is "sentence".

  • After that the string is split into a list of words using split() method and gets stored into a variable "words" i.e. : ["Tutorials", "Point", "is", "a", "good", "place", "for", "learning."].

  • The last word is then extracted using the [-1] index and stored in the variable "last_word". The last word, in this case, is "learning".

  • Finally, the last word i.e., "learning" is printed on the console screen.

So, here with the example "Tutorials Point is a good place to learn" we have learned that how our program actually works which takes the user’s input and prints the last word as output on the console screen in this case it was "Learning".

Use Case of Printing Last Word

The ability to print the last word from a sentence using Python can be useful in many real-life scenarios. Some examples of these scenarios are −

Suppose we are developing a text-processing application that analyzes user feedback on a product. we want to extract the last sentiments expressed in the last word of every sentence. So we can use this method to extract and categorize the feedback of the user and approach them with the relevant product. It also allows us to identify positive, negative, or neutral responses regarding the product.

It is also used in natural language processing like sentiment analysis.

It can be useful in chatbots and virtual assistants by extracting the last word helps in understanding user intent and approaching appropriate responses.

Another use case of extracting the last word from a sentence is text editing and text parsing which helps in auto-correction and syntax highlighting.

Conclusion

Printing the last word in a sentence helps us to extract last word from the sentence. In this article, we have knew about the split() function that is used to separate a sentence into words and allow us to access the last word and how it is useful in different scenarios.

We have understood the step-by-step breakdown of the program. we have also understood the problem with the help of an example. we have also learned about the real-life use case of extracting the last word using Python.

Printing the last word of a sentence can be used in many real-world applications like Natural language processing, chatbots, and virtual assistance. It is very much useful in text processing and text parsing. It can also be used in data extraction and data analysis. This would help us in increasing our knowledge and also help us in improving our programming skills in Python.

Updated on: 04-Oct-2023

292 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements