How to extract the first and final words from a string?

前端 未结 6 1417
鱼传尺愫
鱼传尺愫 2020-12-31 00:49

I have a small problem with something I need to do in school...

My task is the get a raw input string from a user (text = raw_input()) and I need to pri

6条回答
  •  忘掉有多难
    2020-12-31 01:12

    Some might say, there is never too many answer's using regular expressions (in this case, this looks like the worst solutions..):

    >>> import re
    >>> string = "Hello SO user, How are you"
    >>> matches = re.findall(r'^\w+|\w+$', string)
    >>> print(matches)
    ['Hello', 'you']
    

提交回复
热议问题