Python: how to remove only first word from a string
python-2.7
Solution
Python's split has an optional second parameter called `maxsplit`, to specify the largest amount of splits:
line = "Cat Jumped the Bridge"
s2 = line.split(' ', 1)[1]
To quote the docs for `str.split`:
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done
So to explain this code: `str.split(' ', 1)` creates a list with two elements: the first element being the first word (until it reaches a space), and the second being the rest of the string. To only extract the rest of the string, we use `[1]` to indicate the second element.
Note: If you are concerned about having multiple spaces, use `None` as the first parameter for `str.split`, as follows:
line = "Cat Jumped the Bridge"
s2 = line.split(None, 1)[1]
Problem
The input string is given below: ``` line = "Cat Jumped the Bridge" ``` Output should be "Jumped the Bridge". I tried ``` s2 = re.match('\W+.*', line).group() ``` But it returns ``` Traceback (most recent call last): File "regex.py", line 7, in <module> s2 = re.match('\W+.*', line).group() AttributeError: 'NoneType' object has no attribute 'group' ``` So apparently the match failed. Thanks for any suggestions. Joe