Check if String Contains Specific Characters in Python

In this article, we will learn how to check a string for specific characters in Python.

Table Of Contents

Method 1: Using in operator

We can use the in operator to check if a particular character exists in a given string or not. If it exists, the True is returned, otherwise False is returned. Let’s see some examples

Example 1

In this example, we will consider the string – “Welcome to thisPointer” and check for the character – ‘t’ in it.

strValue = "Welcome to thisPointer"

print("String - ", strValue)

# Check for 't' in string strValue
if 't' in strValue:
    print("Character 't' exists in the String")
else:
    print("Character 't' does not exists in the String")

Output:

String -  Welcome to thisPointer
Character 't' exists in the String

We can see that character – ‘t’ is found in strValue.

Example 2

In this example, we will consider the string – “Welcome to thisPointer” and check the existence of multiple characters in it. For ir, we will iterate over all characters in list, and for each character, check if it exists in the string or not. For example,

strValue = "Welcome to thisPointer"

print("String - ", strValue)

# List of characters
listOfCharacters = ['W','e','P','Y']


# Check if characters in the list exists in the above string or not
for ch in listOfCharacters:
    # Check if character ch exists in string strValue
    if ch in strValue:
        print("Character \"", ch, "\" exists in the String")
    else:
        print("Character \"", ch, "\" does not exists in the String")

Output:

String -  Welcome to thisPointer
Character " W " exists in the String
Character " e " exists in the String
Character " P " exists in the String
Character " Y " does not exists in the String

We can see that characters – ‘W’,’e’ and ‘P’ were found in string strValue, but character ‘Y’ was not found in the string.

Method 2: Using contains() method

The contains() method of string class, checks if a particular character exists in the given string or not. If it exists, True is returned, otherwise False is returned.

Example 1 :

In this example, we will consider the string – “Welcome to thisPointer” and check for the character – ‘t’ in it.

strValue = "Welcome to thisPointer"

print("String - ", strValue)

# A character that need to checked
ch = 't'

# Check if character ch exists in string strValue
if strValue.__contains__(ch):
    print("Character \"", ch, "\" exists in the String")
else:
    print("Character \"", ch, "\" does not exists in the String")

Output:

String -  Welcome to thisPointer
Character " t " exists in the String

We can see that character – ‘t’ exists in the string.

Example 2 :

In this example, we will consider the string – “Welcome to thisPointer” and check the existence of particular characters.

strValue = "Welcome to thisPointer"

print("String - ", strValue)

# List of characters
listOfCharacters = ['W','e','P','Y']


# Check if characters in the list exists in the above string or not
for ch in listOfCharacters:
    # Check if character ch exists in string strValue
    if strValue.__contains__(ch):
        print("Character \"", ch, "\" exists in the String")
    else:
        print("Character \"", ch, "\" does not exists in the String")

Output:

String -  Welcome to thisPointer
Character " W " exists in the String
Character " e " exists in the String
Character " P " exists in the String
Character " Y " does not exists in the String

We can see that characters – ‘W’,’e’ and ‘P’ are found in string, but the character ‘Y’ is not found in the string.

Method 2: Using find() method

The string class provides a member function find(). It accepts a string or a character as an argument, and returns the lowest index of the given string/character in the calling string object. If string/character does not exists in the calling string object, then it returns -1.

We can use this to check if a character exists in a string or not. For that, we just need to pass the character in find() function, and check if returned value is not -1. Let’s see some examples,

Example 1 :

In this example, we will consider the string – “Welcome to thisPointer” and check for the character – ‘t’ in it.

strValue = "Welcome to thisPointer"

print("String - ", strValue)

# A character that need to be serached
ch = 't'

# Check if character ch exists in string strValue
if strValue.find(ch) != -1:
    print("Character \"", ch, "\" exists in the String")
else:
    print("Character \"", ch, "\" does not exists in the String")

Output:

String -  Welcome to thisPointer
Character " t " exists in the String

We can see that character – ‘t’ is found in string1.

Example 2 :

In this example, we will consider the string – “Welcome to thisPointer” and check the existence of particular characters.

strValue = "Welcome to thisPointer"

print("String - ", strValue)

# List of characters
listOfCharacters = ['W','e','P','Y']


# Check if characters in the list exists in the above string or not
for ch in listOfCharacters:
    # Check if character ch exists in string strValue
    if strValue.find(ch) != -1:
        print("Character \"", ch, "\" exists in the String")
    else:
        print("Character \"", ch, "\" does not exists in the String")

Output:

String -  Welcome to thisPointer
Character " W " exists in the String
Character " e " exists in the String
Character " P " exists in the String
Character " Y " does not exists in the String

We can see that characters – ‘W’,’e’ and ‘P’ exists in the string, but the character ‘Y’ is not found in the string.

Summary

We learned to check a string for specific character/characters using three different techniques. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top