Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Insensitive Python string split() method

Tags:

python

string

I have 2 strings

a = "abc feat. def"
b = "abc Feat. def"

I want to retrieve the string before the word feat. or Feat.

This is what I'm doing,

a.split("feat.", 1)[0].rstrip()

This returns abc. But how can I perform a case insensitive search using split delimiter?

This is what I've tried so far

b.split("feat." or "Feat.", 1)[0].rstrip()

Output - abc Feat. def

b.split("feat." and "Feat.", 1)[0].rstrip()

Output - abc

a.split("feat." and "Feat.", 1)[0].rstrip()

Output - abc feat. def.

a.split("feat." or "Feat.", 1)[0].rstrip()

Output - abc

Why is this difference with and and or in both the cases?

like image 555
Yin Yang Avatar asked Dec 26 '13 09:12

Yin Yang


People also ask

Is Split string case-sensitive?

Splitting string by mask is case-sensitive. So, be sure to type the characters in the mask exactly as they appear in the source strings.

How do you make a string case insensitive in Python?

Approach No 1: Python String lower() Method This is the most popular approach to case-insensitive string comparisons in Python. The lower() method converts all the characters in a string to the lowercase, making it easier to compare two strings.

What is re split () in Python?

The maxsplit parameter of re. split() is used to define how many splits you want to perform. In simple words, if the maxsplit is 2, then two splits will be done, and the remainder of the string is returned as the final element of the list.

How do you split a string in regex in Python?

If you want to split a string that matches a regular expression (regex) instead of perfect match, use the split() of the re module. In re. split() , specify the regex pattern in the first parameter and the target character string in the second parameter.


2 Answers

Use a regex instead:

>>> import re
>>> regex = re.compile(r"\s*feat\.\s*", flags=re.I)
>>> regex.split("abc feat. def")
['abc', 'def']
>>> regex.split("abc Feat. def")
['abc', 'def']

or, if you don't want to allow FEAT. or fEAT. (which this regex would):

>>> regex = re.compile(r"\s*[Ff]eat\.\s*")
like image 55
Tim Pietzcker Avatar answered Sep 19 '22 15:09

Tim Pietzcker


a[0:a.lower().find("feat.")].rstrip() would do.

anding

"string1" and "string2" and ... and "stringN"

returns the the last string.

oring

"string1" or "string2" or ... or "stringN"

would return the first string.

Short-circuit evaluation.

like image 28
Bleeding Fingers Avatar answered Sep 16 '22 15:09

Bleeding Fingers