Python - Check if String contains Specific Character
Check if String contains Specific Character
To check if given string contains specific character in Python, we can use in membership operator.
Syntax
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.
Example
In the following program, we will take a string name
, and check if the string contains the character 'a'
.
Python Program
x= 'apple'
ch = 'a'
if ch in x :
print('The character is present in string.')
else :
print('The character is not present in string.')
Output
The character is present in string.
Summary
In this tutorial of Python Examples, we learned how to check if a given string contains a specific character, using membership operator, with examples.