Check if a Character is Present in String in Python

Check if Specified Character is in String

To check if a specified character is present in given string in Python, use Python in keyword with If conditional statement.

Code Snippet

The following is a simple code snippet to check if the character ch is in the string name.

if ch in name:
    #code

Example

In the following example, we take a character ch and a string name, and check if the character is present in the string.

Python Program

name = 'apple'
ch = 'p'
if ch in name:
    print(ch, 'is present in', name)
else:
    print(ch, 'is not present in', name)
Run

Output

p is present in apple

We can read the character and the string from user using input() function, and check if the character is present in the string or not.

Python Program

name = input('Enter string : ')
ch = input('Enter character : ')
if ch in name:
    print(ch, 'is present in', name)
else:
    print(ch, 'is not present in', name)

Output#1

Enter string : apple
Enter character : e
e is present in apple

Output#2

Enter string : apple
Enter character : m
m is not present in apple

Summary

In this tutorial of Python Examples, we learned how to check if specific character is present in the string or not, using Python in keyword.