This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 3 years ago.
It can become quite tedious to put multiple variations of spelling into and if statement and I was wondering if there is a way I could make the if statements case insensitive so I only need to put in one thing. I'm just picking up python again and couldn't find a good way online.
In [2]: s = 'aBcDe'
In [3]: if s.lower() == 'abcde':
...: print("look! it's case insensitive")
...:
look! it's case insensitive
Related
This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 1 year ago.
See title. The behavior here is counterintuitive. I think maybe I am missing some flags or something? Why does the regex z not match the string az?
The reason is that match only matches the beginning of a string. Must use search to do the thing that match would do in all other programming languages.
Sorry to throw shade at you Python, but you're the odd one out here.
This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 3 years ago.
I am trying to compare two string and ignore case- sensitive but i don't want to use .lower() or .upper(). Is there another way of doing this?
example1 = "hello"
example2 = "HeLlo"
if example1.casefold()==example2.casefold():
#do something
This will work without needing upper() or lower(). Might not work for non-ASCII characters.
This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 3 years ago.
if "a_string" in random_word or "b_string" in random_word:
...
Is there a cleaner, less dense way to write this boolean?
In case of if, you could use any to check for the occurrence of either of the two strings
if any(i in random_word for i in ["a_string", "b_string"]):
You can use a regular expression.
import re
if re.search(r'a_string|b_string', random_word):
...
This question already has answers here:
Python regex -- extraneous matchings
(5 answers)
Closed 6 years ago.
I searched in all the internet and didnt get a good answer on this thing.
What parentheses in python are stand for? its very wierd..
For example, if i do:
re.split(r'(/s*)', "ho from there")
its will give me a list of separate words with the spaces between that... how does its happening?
This isn't specific to python, but in regex those denote a capture group.
Further information on how these are handled in re.split can be seen here
This question already has answers here:
How to extract the substring between two markers?
(22 answers)
Closed 4 years ago.
I'm looking to build a string function to extract the string contents between two markers. It returns an extraction list
def extract(raw_string, start_marker, end_marker):
... function ...
return extraction_list
I know this can be done using regex but is this fast? This will be called billions of times in my process. What is the fastest way to do this?
What happens if the markers are the same and appear and odd number of times?
The function should return multiple strings if the start and end markers appear more than once.
You probably can't go faster than:
def extract(raw_string, start_marker, end_marker):
start = raw_string.index(start_marker) + len(start_marker)
end = raw_string.index(end_marker, start)
return raw_string[start:end]
But if you want to try regex, just try to benchmark it. There's a good timeit module for it.