{"id":15741,"date":"2021-08-12T09:37:33","date_gmt":"2021-08-12T04:07:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=15741"},"modified":"2021-11-22T18:38:25","modified_gmt":"2021-11-22T13:08:25","slug":"python-program-to-get-the-last-word-from-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-get-the-last-word-from-a-string\/","title":{"rendered":"Python Program to get the Last Word from a String"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Subtract two Complex Numbers<\/a><\/p>\n Given a string that contains the words the task is to print the last word in the given string in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to get the last word from the given string in Python.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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 …<\/p>\nGiven string =hello this is BTechgeeks<\/pre>\n
The last word in the given string { hello this is BTechgeeks } is: BTechgeeks<\/pre>\n
Given string =good morning this is btechgeeks<\/pre>\n
The last word in the given string { good morning this is btechgeeks } is: btechgeeks<\/pre>\n
Program to get the Last Word from a String in Python<\/h2>\n
\n
Method #1: Using split() Method (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvnstrng = 'hello this is BTechgeeks'\r\n# Split the words of the given string to a list of words\r\n# using the built-in split() function.\r\nlsttofwrds = gvnstrng.split()\r\n# Get the last word from the above list of words using negative indexing\r\n# and store it in a variable.\r\nlstwrd = lsttofwrds[-1]\r\n# Print the last word of the given string by printing the above variable.\r\nprint('The last word in the given string {', gvnstrng, '} is:', lstwrd)\r\n<\/pre>\n
The last word in the given string { hello this is BTechgeeks } is: BTechgeeks<\/pre>\n
Method #2: Using split() Method (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in the variable.\r\ngvnstrng = input('Enter some random string = ')\r\n# Split the words of the given string to a list of words\r\n# using the built-in split() function.\r\nlsttofwrds = gvnstrng.split()\r\n# Get the last word from the above list of words using negative indexing\r\n# and store it in a variable.\r\nlstwrd = lsttofwrds[-1]\r\n# Print the last word of the given string by printing the above variable.\r\nprint('The last word in the given string {', gvnstrng, '} is:', lstwrd)\r\n<\/pre>\n
Enter some random string = good morning this is btechgeeks\r\nThe last word in the given string { good morning this is btechgeeks } is: btechgeeks<\/pre>\n
\n