Case-insensitive string comparison without using lower or upper [duplicate] - python

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.

Related

String replace() vs string translate() method [duplicate]

This question already has answers here:
How to replace two things at once in a string?
(6 answers)
using .replace to replace more than one character in python [duplicate]
(4 answers)
Closed 2 years ago.
I have a question regarding my code below:
Input: A DNA string Pattern (ex: 'AAAACCCGGT')
Output: The complementary string (ex: 'TTTTGGGCCA')
def Complement(Pattern):
comPattern=Pattern.translate(str.maketrans({'A':'T','T':'A','G':'C','C':'G'}))
return comPattern
I tried using str.replace() method multiple times for above problem, but it did not work. Any idea why?

Python. Variable in regular expression to find continuous (repeated) sequences of same string [duplicate]

This question already has answers here:
How to use a variable inside a regular expression?
(12 answers)
Closed 2 years ago.
I need find a repeated sequences of same my_string = 'ABCD' in string my_data = 'ABCDABCDRRRABCD' and return the longest one 'ABCDABCD'.
If I use result = ([max(i) for i in re.findall(r'((ABCD)\2+)', data)]) It's works fine, but I can't put a variable my_string so I can use it for difference strings in some loop.
To put variable inside of regular expression you can use .format
result = ([max(i) for i in re.findall(r'(({0})\2+)'.format(my_string), my_data)])

Is there a better way to do booleans? [duplicate]

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):
...

Detect what a python string begins with [duplicate]

This question already has answers here:
Checking whether a string starts with XXXX
(5 answers)
Closed 5 years ago.
To detect whether a python string ends with a particular substring, say ".txt", there is a convenient built-in python string method;
if file_string.endswith(".txt"):
I would like to to detect whether a python string begins with a particular substring, say "begin_like_this". I am not able find a convenient method that I can use like this;
if file_string.beginswith("begin_like_this"):
I am using Python 3.6.
You're looking for str.startswith
if file_string.startswith("begin_like_this"):

How can i make a string case insensitive in Python? [duplicate]

This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 8 years ago.
For example i am given this string as an input:
"HeLlo"
How can i make this case insensitive for later uses?
I want it to be equal to "hello" or "HELLo" etc...
You have the "your string".upper() or "your string".lower() functions, which will allow you to compare the strings, assuring the comparison is case insensitive.

Categories