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):
...
Related
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:
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
This question already has answers here:
How to get the size of a string in Python?
(6 answers)
Closed 7 years ago.
(New to python and stack overflow)
I was curious if there was a way to count the amount of letters in a string for python. for example:
string="hello"
I just want something to count the letters then output it into a variable for later use.
The following will give the length of a string:
len(string)
In your case, you can assign it:
numLetters = len(string)
This function can be used for other objects besides strings. For additional uses, read the documentation.
Use python function len, i.e.:
size = len(string)
len()
https://docs.python.org/2/library/functions.html#len
DEMO
https://ideone.com/mhpdLi
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.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Python strings split with multiple separators
Is there a way in Python to split a string on two different keys?
Say I have a string like this:
mystr = "wketjwlektjwltjkw<br/>wwwweltjwetlkwjww" + \
"wwetlwjtwlet<strong>wwwwketjwlektjwlk</strong"
I'd like to split on EITHER <br/>wwww or <strong>wwww.
How should I do this?
Thanks!
import re
re.split(r'<(br\/|strong)>wwww', mystr)[::2]