Remove the first word in a Python string?
Question:
What’s the quickest/cleanest way to remove the first word of a string? I know I can use split
and then iterate on the array to get my string. But I’m pretty sure it’s not the nicest way to do it.
Answers:
I think the best way is to split, but limit it to only one split by providing maxsplit
parameter:
>>> s = 'word1 word2 word3'
>>> s.split(' ', 1)
['word1', 'word2 word3']
>>> s.split(' ', 1)[1]
'word2 word3'
Presuming you can guarantee the words are separated by a single space, str.partition()
is what you are looking for.
>>> test = "word1 word2 word3"
>>> test.partition(" ")
('word1', ' ', 'word2 word3')
The third item in the tuple is the part you want.
The other answer will raise an exception if your string only has one word, which I presume is not what you want.
One way to do this instead is to use the str.partition
function.
>>> s = "foo bar baz"
>>> first, _, rest = s.partition(" ")
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition(" ")
>>> rest or first
'foo'
A naive solution would be:
text = "funny cheese shop"
print text.partition(' ')[2] # cheese shop
However, that won’t work in the following (admittedly contrived) example:
text = "Hi,nice people"
print text.partition(' ')[2] # people
To handle this, you’re going to need regular expressions:
import re
print re.sub(r'^W*w+W*', '', text)
More generally, it’s impossible to answer a question involving “word” without knowing which natural language we’re talking about. How many words is “J’ai”? How about “中华人民共和国”?
What’s the quickest/cleanest way to remove the first word of a string? I know I can use split
and then iterate on the array to get my string. But I’m pretty sure it’s not the nicest way to do it.
I think the best way is to split, but limit it to only one split by providing maxsplit
parameter:
>>> s = 'word1 word2 word3'
>>> s.split(' ', 1)
['word1', 'word2 word3']
>>> s.split(' ', 1)[1]
'word2 word3'
Presuming you can guarantee the words are separated by a single space, str.partition()
is what you are looking for.
>>> test = "word1 word2 word3"
>>> test.partition(" ")
('word1', ' ', 'word2 word3')
The third item in the tuple is the part you want.
The other answer will raise an exception if your string only has one word, which I presume is not what you want.
One way to do this instead is to use the str.partition
function.
>>> s = "foo bar baz"
>>> first, _, rest = s.partition(" ")
>>> rest or first
'bar baz'
>>> s = "foo"
>>> first, _, rest = s.partition(" ")
>>> rest or first
'foo'
A naive solution would be:
text = "funny cheese shop"
print text.partition(' ')[2] # cheese shop
However, that won’t work in the following (admittedly contrived) example:
text = "Hi,nice people"
print text.partition(' ')[2] # people
To handle this, you’re going to need regular expressions:
import re
print re.sub(r'^W*w+W*', '', text)
More generally, it’s impossible to answer a question involving “word” without knowing which natural language we’re talking about. How many words is “J’ai”? How about “中华人民共和国”?