-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
  • 897
  • 2
  • 12
  • 28

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
  • 78,594
  • 17
  • 67
  • 103
1

You can use regex

import re
x = "this is ;a cool test"
x = re.sub(r'^[^;]+;','',x)
print(x)
mkHun
  • 5,693
  • 2
  • 33
  • 72