-4

Assuming I have the following string:

this is ;a cool test

How can I remove everything from the start up to the first time a ; occurs? The expected output would be a cool test.

I only know how to remove a fixed amount of characters using the bracket notation, which is not helpful here because the location of the ; is not fixed.

Kyu96
  • 1,159
  • 2
  • 17
  • 35

2 Answers2

2

Using str.find and slicing.

Ex:

s = "this is ;a cool test; Hello World."
print(s[s.find(";")+1:])
# --> a cool test; Hello World.

Or using str.split

Ex:

s = "this is ;a cool test; Hello World."
print(s.split(";", 1)[-1])
# --> a cool test; Hello World.
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

You can use regex

import re
x = "this is ;a cool test"
x = re.sub(r'^[^;]+;','',x)
print(x)
mkHun
  • 5,891
  • 8
  • 38
  • 85