Keep complete string after the 1st split in Python

Rahul Agarwal

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

jpp

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Python Split string after 2000 characters

From Java

Will PIP work for python 2.7 after its End of Life on 1st Jan 2020

From Dev

Get the 1st word of the string with RegEx

From Dev

How to terminate shell on 1st instance of failed command ( search for 1st instance of string and append to it )

From Dev

How to split a string and keep the delimiters?

From Dev

java for loop 1st char of 1st string then last char of second string

From Dev

How to regex split, but keep the split string?

From Dev

Split string after : and before?

From Dev

Python Regex - Split string after every character

From Dev

Keep string after first number

From Dev

How to split a String and keep the delimiter '='

From Dev

Will PIP work for python 2.7 after its End of Life on 1st Jan 2020

From Dev

How to terminate shell on 1st instance of failed command ( search for 1st instance of string and append to it )

From Dev

why cannot invoke remove directly after split a string in python?

From Dev

How do I split or grab a string in python after a comma in python

From Dev

java for loop 1st char of 1st string then last char of second string

From Dev

Split a string and keep separator string in c#

From Dev

VBA How to keep a specific comma after string split

From Dev

Remove everything after 1st opening bracket incl. the opening bracket - in a string - PHP

From Dev

get the string after check 2nd comma separated string not in 1st comma separated string

From Dev

(SQL Server) return sub-string value after 1st occurrence of 'character'

From Dev

Foreach(){} stops after 1st row

From Dev

Excel keep 1st three rows and delete rest of duplicates

From Dev

split string on commas but ignore commas with in single quotes and create a dictionary after string split in python

From Dev

Disable GetTouch after 1st touch

From Dev

Split a long string by using a complete word

From Dev

Python: Split a string into different elements after a ,

From Dev

Split String and keep null value

From Dev

Take the containing string array after split in Python

Related Related

  1. 1

    Python Split string after 2000 characters

  2. 2

    Will PIP work for python 2.7 after its End of Life on 1st Jan 2020

  3. 3

    Get the 1st word of the string with RegEx

  4. 4

    How to terminate shell on 1st instance of failed command ( search for 1st instance of string and append to it )

  5. 5

    How to split a string and keep the delimiters?

  6. 6

    java for loop 1st char of 1st string then last char of second string

  7. 7

    How to regex split, but keep the split string?

  8. 8

    Split string after : and before?

  9. 9

    Python Regex - Split string after every character

  10. 10

    Keep string after first number

  11. 11

    How to split a String and keep the delimiter '='

  12. 12

    Will PIP work for python 2.7 after its End of Life on 1st Jan 2020

  13. 13

    How to terminate shell on 1st instance of failed command ( search for 1st instance of string and append to it )

  14. 14

    why cannot invoke remove directly after split a string in python?

  15. 15

    How do I split or grab a string in python after a comma in python

  16. 16

    java for loop 1st char of 1st string then last char of second string

  17. 17

    Split a string and keep separator string in c#

  18. 18

    VBA How to keep a specific comma after string split

  19. 19

    Remove everything after 1st opening bracket incl. the opening bracket - in a string - PHP

  20. 20

    get the string after check 2nd comma separated string not in 1st comma separated string

  21. 21

    (SQL Server) return sub-string value after 1st occurrence of 'character'

  22. 22

    Foreach(){} stops after 1st row

  23. 23

    Excel keep 1st three rows and delete rest of duplicates

  24. 24

    split string on commas but ignore commas with in single quotes and create a dictionary after string split in python

  25. 25

    Disable GetTouch after 1st touch

  26. 26

    Split a long string by using a complete word

  27. 27

    Python: Split a string into different elements after a ,

  28. 28

    Split String and keep null value

  29. 29

    Take the containing string array after split in Python

HotTag

Archive