This post will discuss how to check a string for a specific character in Python.

1. Using in operator

The Pythonic, fast way to check for the specific character in a string uses the in operator. It returns True if the character is found in the string and False otherwise.

Download  Run Code

 
The in operator implement the built-in function __contains__. It is not a good practice to call directly.

Download  Run Code

2. Using str.find() function

Another common approach to search for a character in the string is using the str.find() function. This returns the index of the first occurrence of the character in the string and -1 when the character is not found.

Download  Run Code

3. Using Regex

Another approach is to use the regex, as demonstrated below. Note that regular expressions are costly and should be avoided whenever possible.

Download  Run Code

That’s all about checking a string for a specific character in Python.