Remove the first word in a Python string?

后端 未结 4 1723

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 s

4条回答
  •  时光说笑
    2020-12-25 11:05

    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 "中华人民共和国"?

提交回复
热议问题