I have strings like:
a = "RT@xyz: AI is awesome"
req_ans = " AI is awesome"
b = "RT@xyz: AI: is awesome"
req_ans=" AI: is awesome"
c = "RT@xyz: AI: is: awesome"
req_ans=" AI: is: awesome"
So, my requirement is to split the string after 1st column and keep the rest of the string.
I have tried below code but it works for "a" type of string but fails for "b" and "c" type:
req_ans= a.split(':')[1]
Also tried:
req_ans= a.split(':')[1:]
But it converts the string into lists but I want the remaining string which is left after the first ":"
python split by number of times specified checked the above answer but it converts it to list of words, which I don't want and hence my question is different
Just use str.join
after str.split
:
req_ans = ':'.join(c.split(':')[1:])
# ' AI: is: awesome'
The result of str.split
is a list. If you want all the components after a certain character, without removing the split character, you need to reverse the split after slicing.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments