Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

Remove the first word in a Python string?

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.

Ps: I'm quite new to python and I don't know every trick.

Thanks in advance for your help.

question from:https://stackoverflow.com/questions/12883376/remove-the-first-word-in-a-python-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...