Python Program to Remove the last Word from the String/Sentence

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()  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.

pop() Function in Python:

Python’s list pop() function removes and returns the final value from a List or the supplied index value.

Examples:

Example1:

Input:

Given String = "Hello this is btechgeeks"

Output:

The given string{ Hello this is btechgeeks } after removal of last word = Hello this is

Example2:

Input:

Given String = "Good Morning btechgeeks Hello everyone"

Output:

The given string{ Good Morning btechgeeks Hello everyone } after removal of last word =  Good Morning btechgeeks Hello

Program to Remove the last Word from the String/Sentence in Python

Below are the ways to remove the last word from a given string/sentence:

Method #1: Using pop() Function (Static Input)

Approach:

  • Give the string/sentence as static input and store it in a variable.
  • Convert the given string into a list of words using the list() and split() functions and store it in another variable.
  • Apply pop() function on the above list of words.
  • Convert the list of words into the string using the join() function and store it in another variable.
  • Print the String after removal of the last word from the above-given string.
  • The Exit of the program.

Below is the implementation:

# Give the string/sentence as static input and store it in a variable.
gven_strng = 'Hello this is btechgeeks'
# Convert the given string into a list of words using the list() and split() functions
# and store it in another variable.
str_lst = list(gven_strng.split())
# Apply pop() function on the above list of words.
str_lst.pop()
# Convert the list of words into the string using the join() function and store it in
# another variable.
modified_str = ' '.join(str_lst)
# Print the String after removal of the last word from the above-given string.
print("The given string{", gven_strng,
      "} after removal of last word = ", modified_str)

Output:

The given string{ Hello this is btechgeeks } after removal of last word =  Hello this is

Method #2: Using pop() Function (User Input)

Approach:

  • Give the string/sentence as user input using the input() function and store it in a variable.
  • Convert the given string into a list of words using the list() and split() functions and store it in another variable.
  • Apply pop() function on the above list of words.
  • Convert the list of words into the string using the join() function and store it in another variable.
  • Print the String after removal of the last word from the above-given string.
  • The Exit of the program.

Below is the implementation:

# Give the string/sentence as user input using the input() function and
# store it in a variable.
gven_strng = input("Enter some random string = ")
# Convert the given string into a list of words using the list() and split() functions
# and store it in another variable.
str_lst = list(gven_strng.split())
# Apply pop() function on the above list of words.
str_lst.pop()
# Convert the list of words into the string using the join() function and store it in
# another variable.
modified_str = ' '.join(str_lst)
# Print the String after removal of the last word from the above-given string.
print("The given string{", gven_strng,
      "} after removal of last word = ", modified_str)

Output:

Enter some random string = Good Morning btechgeeks hello every one
The given string{ Good Morning btechgeeks hello every one } after removal of last word = Good Morning btechgeeks hello every

Enhance your coding skills with our list of Python Basic Programs provided and become a pro in the general-purpose programming language Python in no time.