Python Basics

Check if a python string contains a character

In some cases we may want to check if string contains a certain character. To do check presence of a character in a string in python, we can use the in keyword functionality. If element is present then it would return true other wise it would return false.

Example

sentence=”Orange is not apple”

#lets check if ‘k’ is present in the sentence

if ‘k’ in sentence:

print(“yes k is present”)

else:

print(“k is not present”)

Output: K is not present.

Example

#lets check if ‘a’ is present in the sentence

if ‘k’ in sentence:

print(“yes a is present”)

else:

print(“a is not present”)

Output: yes a is present