{"id":20027,"date":"2021-09-06T13:33:15","date_gmt":"2021-09-06T08:03:15","guid":{"rendered":"https:\/\/python-programs.com\/?p=20027"},"modified":"2021-11-22T18:36:29","modified_gmt":"2021-11-22T13:06:29","slug":"python-program-to-remove-the-last-word-from-the-string-sentence","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-remove-the-last-word-from-the-string-sentence\/","title":{"rendered":"Python Program to Remove the last Word from the String\/Sentence"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Print Palindrome Numbers in a Range<\/a><\/p>\n Given a string\/sentence and the task is to remove the last word from the given sentence.<\/p>\n join()<\/strong> :<\/p>\n The string method join()\u00a0 takes all of the items in an iterable and returns a string after joining them all together. Iterable types include list, tuple, string, dictionary, and set.<\/p>\n pop() Function in Python:<\/strong><\/p>\n Python’s list pop() function removes and returns the final value from a List or the supplied index value.<\/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 remove the last word from a given string\/sentence:<\/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 Enhance your coding skills with our list of Python Basic Programs<\/a> provided and become a pro in the general-purpose programming language Python in no time.<\/p>\n In the previous article, we have discussed Python Program to Print Palindrome Numbers in a Range Given a string\/sentence and the task is to remove the last word from the given sentence. join() : The string method join()\u00a0 takes all of the items in an iterable and returns a string after joining them all together. …<\/p>\nGiven String = \"Hello this is btechgeeks\"<\/pre>\n
The given string{ Hello this is btechgeeks } after removal of last word = Hello this is<\/pre>\n
Given String = \"Good Morning btechgeeks Hello everyone\"<\/pre>\n
The given string{ Good Morning btechgeeks Hello everyone } after removal of last word = Good Morning btechgeeks Hello<\/pre>\n
Program to Remove the last Word from the String\/Sentence in Python<\/h2>\n
\n
Method #1: Using pop() Function (Static Input)<\/h3>\n
\n
# Give the string\/sentence as static input and store it in a variable.\r\ngven_strng = 'Hello this is btechgeeks'\r\n# Convert the given string into a list of words using the list() and split() functions\r\n# and store it in another variable.\r\nstr_lst = list(gven_strng.split())\r\n# Apply pop() function on the above list of words.\r\nstr_lst.pop()\r\n# Convert the list of words into the string using the join() function and store it in\r\n# another variable.\r\nmodified_str = ' '.join(str_lst)\r\n# Print the String after removal of the last word from the above-given string.\r\nprint(\"The given string{\", gven_strng,\r\n \"} after removal of last word = \", modified_str)\r\n<\/pre>\n
The given string{ Hello this is btechgeeks } after removal of last word = Hello this is<\/pre>\n
Method #2: Using pop() Function (User Input)<\/h3>\n
\n
# Give the string\/sentence as user input using the input() function and\r\n# store it in a variable.\r\ngven_strng = input(\"Enter some random string = \")\r\n# Convert the given string into a list of words using the list() and split() functions\r\n# and store it in another variable.\r\nstr_lst = list(gven_strng.split())\r\n# Apply pop() function on the above list of words.\r\nstr_lst.pop()\r\n# Convert the list of words into the string using the join() function and store it in\r\n# another variable.\r\nmodified_str = ' '.join(str_lst)\r\n# Print the String after removal of the last word from the above-given string.\r\nprint(\"The given string{\", gven_strng,\r\n \"} after removal of last word = \", modified_str)\r\n<\/pre>\n
Enter some random string = Good Morning btechgeeks hello every one\r\nThe given string{ Good Morning btechgeeks hello every one } after removal of last word = Good Morning btechgeeks hello every<\/pre>\n
\n