xxxxxxxxxx
string.isalpha() it returns boolean value
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