How to check if a character exists in a string with Python

1 Answer

0 votes
# Define the string we want to search in
s: str = "python"

# Check if a character exists using the idiomatic 'in' operator
exists_h = 'h' in s      # True because 'h' is in "python"
exists_z = 'z' in s      # False because 'z' is not in "python"

# Print the results of the checks
print(exists_h)
print(exists_z)

# Use a conditional to print a message
if 'h' in s:
    print("exists")
else:
    print("not exists")



'''
run:

True
False
exists

'''

 



answered 1 hour ago by avibootz
...