darkhorse
darkhorse

Reputation: 8722

How to remove characters after 2 certain characters in a Python string?

Lets say I have a bunch of strings, and they can only be in the following formats:

format1 = 'substring1#substring2'
format2 = 'substring1$substring2'
format3 = 'substring1'

Let me explain. The strings are sometimes divided using the # or $ character. However other times, they are not.

I want to remove the part that appears after the # or $, if it exists. If it was just one special character, that is #, I could have done this:

string = string.split('#')[0]

But how can I do it for the 2 special characters in a quick and elegant way? Also assume the following things:

  1. Only one special character can appear in the string.
  2. The special characters will not appear in any other part of the string.

Thanks.

Upvotes: 0

Views: 91

Answers (7)

Iulian
Iulian

Reputation: 300

I think you can use a list to maintain the special characters that can be used and for every of them check if is present in the string, when you find one execute the splitting process and retrieve only the left part like so:

delimters = ["#","$"]

for symbol in delimters:
  if symbol in string1:
    left_part = string1.split(symbol)[0]

Now this approach has some disadvantages but is the simplest in my opinion. The problem is that if you have more than one string you need to nested loops.

Upvotes: 0

jpp
jpp

Reputation: 164673

You can use a for loop to split by an arbitrary number of delimiters. Regular expression is typically less efficient than Python str methods.

def converter(x, delims='#$'):
    for delim in delims:
        x = x.split(delim, maxsplit=1)[0]
    return x

format1 = 'substring1#substring2'
format2 = 'substring1$substring2'
format3 = 'substring1'

for value in [format1, format2, format3]:
    print(converter(value))

# substring1
# substring1
# substring1

Upvotes: 0

Dani Mesejo
Dani Mesejo

Reputation: 61910

Use replace before split:

format1 = 'substring1#substring2'
format2 = 'substring1$substring2'
format3 = 'substring1'

print(format1.replace('#', '$').split('$')[0])
print(format2.replace('#', '$').split('$')[0])
print(format3.replace('#', '$').split('$')[0])

Output

substring1
substring1
substring1

Upvotes: 0

ipave
ipave

Reputation: 1338

Use re.split() for it.

import re 
print(re.split("#|$","STRING#OTHER_STRING#OTHER_STRING_2"))

Upvotes: 1

glibdud
glibdud

Reputation: 7840

If you want to avoid regex, one possibility would be:

string = min(string.split('#')[0], string.split('$')[0])

Upvotes: 2

adrtam
adrtam

Reputation: 7211

Use regex!

import re
new_string = re.sub(r"(#|$).*$", "", string)

Upvotes: 2

Richard
Richard

Reputation: 61289

Regular expressions.

import re
re.sub('[$#].*', '', string_to_modify)

Upvotes: 2

Related Questions