Python - Print the last word in a sentence
Last Updated :
16 Jan, 2025
Printing the last word in a sentence involves extracting the final word , often done by splitting the sentence into words or traversing the string from the end.
Using split() method
split()
method divides the string into words using spaces, making it easy to access the last word by retrieving the last element of the resulting list.
Python
s = "Learn algorithms at geeksforgeeks"
res= s.split()[-1]
print(res)
Explanation:
s.split()
splits the string s into a list of words ['Learn', 'algorithms', 'at', 'geeksforgeeks']
.[-1]
accesses the last word "geeksforgeeks"
.res
stores the last word, which is then printed.
Using rsplit()
rsplit()
method is similar to split()
but it splits the string starting from the right side. By limiting the number of splits to 1, it avoids unnecessary work and makes it more efficient for extracting the last word.
Python
s = "Learn algorithms at geeksforgeeks"
res = s.rsplit(" ", 1)[-1]
print(res)
Explanation:
- rsplit(" ", 1) splits the string s from the right at the last space encountered. By passing the argument 1 ,it limits the number of splits to one.
[-1]
accesses the last element of the list which is "geeksforgeeks"
.
Using rstrip()
rstrip()
method removes trailing spaces ensuring no unnecessary space causes errors. Then by traversing the string from the end, it identifies and returns the last word.
Python
s= "Learn algorithms at geeksforgeeks"
s= s.rstrip() # Remove trailing spaces
i = len(s) - 1
# Traverse from the end of the string to find the last word
while i >= 0 and s[i] != " ":
i -= 1
res = s[i + 1:]
print(res)
Explanation:
rstrip()
removes trailing spaces from the string s- while i >= 0 and s[i] != " ": This loop starts from the end of the string s moving backward to find the first space.
string[i + 1:]
extracts the substring after the space, which is the last word
Using regular expression
Regular expressions are powerful tools for pattern matching. Here \b\w+\b
matches whole words and the findall()
method retrieves all words in the sentence. Last word is simply the last element in the result.
Python
import re
s = "Learn algorithms at geeksforgeeks"
# Find all words and return the last one
res = re.findall(r'\b\w+\b', s)[-1]
print(res)
Explanation:
re.findall(r'\b\w+\b', string)
: This regular expression matches all words in the string. Here \b
represents a word boundary and \w+
matches one or more word characters.[-1]
:This extracts the last word from the list of words found by findall()
.
Using loop
In this approach, we manually traverse the string from the end and building the last word character by character until a space is encountered. Once the word is fully captured, it is returned without needing to reverse it.
Python
s= "Learn algorithms at geeksforgeeks"
res = ""
# Traverse the string from the end and build the last word
for i in range(len(s) - 1, -1, -1):
if s[i] == " ":
break
res = s[i] + res
print(res)
Explanation:
for i in range(len(s) - 1, -1, -1)
: This iterates backward through the string s from the last character to the first.if s[i] == " "
stops the loop when a space is encountered, marking the end of the last word.res = s[i] + res
adds characters to res
in reverse order, building the last word.