xxxxxxxxxx
def check_string_contains_only(s, allowed_chars):
"""Checks if a given string contains only certain characters."""
for char in s:
if char not in allowed_chars:
return False
return True
# Example usage:
string = "Hello World"
allowed_chars = "HeloWrd" # Allowed characters
result = check_string_contains_only(string, allowed_chars)
print(result) # Output: False
allowed_chars = "Helo Wrld" # Allowed characters
result = check_string_contains_only(string, allowed_chars)
print(result) # Output: True
xxxxxxxxxx
myString = "<text contains this>"
myOtherString = "AnotherString"
# Casting to string is not needed but it's good practice
# to check for errors
if str(myString) in str(myOtherString):
# Do Something
else:
# myOtherString didn't contain myString
xxxxxxxxxx
if any(ext in url_string for ext in extensionsToCheck):
print(url_string)
xxxxxxxxxx
import timeit
def func1():
phrase = 'Lucky Dog'
return any(i in 'LD' for i in phrase)
def func2():
phrase = 'Lucky Dog'
if ('L' in phrase) or ('D' in phrase):
return True
else:
return False
if __name__ == '__main__':
func1_time = timeit.timeit(func1, number=100000)
func2_time = timeit.timeit(func2, number=100000)
print('Func1 Time: {0}\nFunc2 Time: {1}'.format(func1_time, func2_time))