i am trying to delete certain portion of a string if a match found in the string as below
string = 'Newyork, NY'
I want to delete all the characters after the comma from the string including comma, if comma is present in the string
Can anyone let me now how to do this .
Use
.split():We split the string on the comma once, to save python the work of splitting on more commas.
Alternatively, you can use
.partition():Demo:
.partition()is the faster method: