Last modified: Jun 05, 2023 By Alexander Williams
Remove First or Last Word From String Python
In Python, you can use various string manipulation techniques to remove the first or the last word from a string. Here are a few approaches:
Using the split() and join() methods
The split()
function is a built-in function in Python that splits a string into a list of substrings based on a specified delimiter. And, The join()
function is a built-in function that concatenates elements of an iterable,
we can effectively remove the first or the last word from a given string.
Remove the First Word From the String
Here is an example:
string = "Hello World" # String
words = string.split() # Split the string
new_string = ' '.join(words[1:]) # Join the words
print(new_string)
Output:
Hello
Here's an explanation of the code in a step-by-step:
- Initialize the string
- Split the string into a list of words
- Create a new string by joining the words starting from the second word
- Print the new string
Remove the last word from the string
To remove the last word, we'll join the words excluding the last word using this code ' '.join(words[:-1])
. See the example:
string = "Hello World"
words = string.split()
new_string = ' '.join(words[:-1]) # Join the words excluding the last word
print(new_string)
Output:
Hello
Using the find() and string slicing
In Python, find()
is a built-in string method that allows you to search for a substring within a string. It returns the index of the first occurrence of the substring if found or -1 if the substring is not present.
Remove the first word from the string
string = "Hello World"
space_index = string.find(' ') # Find the index of the first space character
new_string = string[space_index+1:] # Extract a slice of the string
print(new_string)
In the code above, we:
- Find the index of the first space character
- Extract a slice of the string starting from the character after the space
Output:
World
Remove the last word from the string
string = "Hello World"
space_index = string.rfind(' ') # Find the index of the last space character
new_string = string[:space_index]
print(new_string)
In this example, we extract a slice of the string from the start to the character before the last space.
Output:
Hello
Using regular expressions (regex)
Using regular expressions, we can also remove the first or last word from the string. Regular expressions, commonly called "regex" or "regexp," are powerful tools for pattern matching and text manipulation.
Remove the first Word from the string
import re
string = "Hello World"
new_string = re.sub(r'^\w+\s', '', string)
print(new_string)
Output:
World
The line re.sub(r'^\w+\s', '', string)
uses the re.sub()
function from the re
module in Python to substitute a pattern with a replacement in a given string.
The regular expression pattern r'^\w+\s'
matches a word followed by a whitespace character at the beginning of a string.
Remove the last word from the string
import re
string = "Hello World"
new_string = re.sub(r'\s\w+$', '', string)
print(new_string)
Output:
Hello
Here, the regular expression pattern r'\s\w+$'
matches a whitespace character followed by a word at the end of a string.
Conclusion
In conclusion, removing the first or last word from a string in Python can be achieved using various string manipulation techniques.
Whether you split the string into words, utilize string slicing, or employ regular expressions, the goal remains: to extract and reconstruct the desired portion without the targeted word.
If you need to extract the first, second, or last word from a string, refer to the resource Get First, Second, Last word in String.