Check if String contains Specific Character
To check if given string contains specific character in Python, we can use in membership operator.
The syntax of the expression to check if the string x contains the character ch is
ch in x
Since there is no exclusive datatype for a character in Python, we may specify the character in a string literal.
In the following program, we will take a string name, and check if the string contains the character 'a'.
Run Code Copy
x= 'apple' ch = 'a' if ch in x : print('The character is present in string.') else : print('The character is not present in string.')
The character is present in string.
In this tutorial of Python Examples, we learned how to check if a given string contains a specific character, using membership operator, with examples.