Python Program to Remove the Last Word from String

Given a string and the task is to remove the last word from the given string.

split() method :

Splitting a string into a list is accomplished by the split() method.

join() function:

The join() method is a string method that returns a string containing the elements of a sequence that have been joined by a string separator.

Examples:

Example1:

Input:

Given string = "good morning this is btechgeeks hello all"

Output:

The given string after removal of last word from a string:
good morning this is btechgeeks hello

Example2:

Input:

Given string = "Hello this is btechgeeks "

Output:

The given string after removal of last word from a string: Hello this is

Program to Remove the Last Word from String

Below are the ways to remove the last word from the given string.

Method #1: Using Slicing (Static input)

Approach:

  • Give the string as static input and store it in a variable.
  • Split the given string separated by spaces using the split function() and store it in another variable.
  • Remove the last word from a given string using the slicing and store it in another variable.
  • Convert the above-obtained list to string using the join() function and store it in a variable.
  • Print the given string after removal of the last word from a string.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "Hello this is btechgeeks "
# Split the given string separated by spaces using the split function()
# and store it in another variable.
splt_str = gvn_str.split()
# Remove the last word from a string using the slicing and store it in another variable.
rmove_lst_wrd = splt_str[:-1]
# Convert the above-obtained list to string using the join() function and
# store it in a variable.
fnl_str = ' '.join(rmove_lst_wrd)
print("The given string after removal of last word from a string:")
# Print the given string after removal of the last word from a string.
print(fnl_str)

Output:

The given string after removal of last word from a string:
Hello this is

Method #2: Using Slicing (User input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Split the given string separated by spaces using the split function() and store it in another variable.
  • Remove the last word from a given string using the slicing and store it in another variable.
  • Convert the above-obtained list to string using the join() function and store it in a variable.
  • Print the given string after removal of the last word from a string.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Split the given string separated by spaces using the split function()
# and store it in another variable.
splt_str = gvn_str.split()
# Remove the last word from a string using the slicing and store it in another variable.
rmove_lst_wrd = splt_str[:-1]
# Convert the above-obtained list to string using the join() function and
# store it in a variable.
fnl_str = ' '.join(rmove_lst_wrd)
print("The given string after removal of last word from a string:")
# Print the given string after removal of the last word from a string.
print(fnl_str)

Output:

Enter some random string = good morning this is btechgeeks hello all
The given string after removal of last word from a string:
good morning this is btechgeeks hello