How to scan a string for specific characters in Python?


In this article, we are going to find out how to scan a string for specific characters in Python.

The first approach is by using the ‘in’ keyword. We can use the ‘in’ keyword for checking if a character is present in the string. If it is present in the string, True is returned otherwise, False is returned.

The ‘in’ keyword is also used to iterate through a sequence in a for loop which is also used for iterating over a sequence like list, tuple and string or other iterable objects.

Example 1

In the example given below, we are taking a string as input and we are checking if a specific character is present in the string or not using the ‘in’ operator 

str1 = "Welcome to Tutorialspoint"
char = 'T'

print("The given string is")
print(str1)

print("The given character is")
print(char)

print("Checking if the character is present in the string")
print(char in str1)

Output

The output of the above example is as follows −

The given string is
Welcome to Tutorialspoint
The given character is
T
Checking if the character is present in the string
True

Example 2

In the example given below, we are taking the same program as above but we are taking a different input and checking if that specific character is present in the string 

str1 = "Welcome to Tutorialspoint"
char = 'z'

print("The given string is")
print(str1)

print("The given character is")
print(char)

print("Checking if the character is present in the string")
print(char in str1)

Output

The output of the above example is given below −

The given string is
Welcome to Tutorialspoint
The given character is
z
Checking if the character is present in the string
False

Using Sets

The second approach is by using Sets. We will take a set of characters and we will check if any of the characters from the set is present in the string using the ‘any’ method. This method returns True if any of the character from the set is present in the string otherwise, False is returned.

Example 1

In the example given below, we are taking a string as input and a set of characters and we are checking if any of the characters are present in the string 

str1 = "Welcome to Tutorialspoint123"
chars = set('0123456789$,')

print("The given string is")
print(str1)

print("The given character is")
print(chars)

print("Checking if any of the characters is present in the string")
print(any((c in chars) for c in str1))

Output

The output of the above example is as shown below −

The given string is
Welcome to Tutorialspoint123
The given character is
{'9', '1', ',', '5', '$', '3', '7', '2', '0', '6', '8', '4'}
Checking if any of the characters is present in the string
True

Example 2

In the example given below, we are taking the same program as above but with different input string and checking if the string contains the given characters −

str1 = "Welcome to Tutorialspoint"
chars = set('0123456789$,')

print("The given string is")
print(str1)

print("The given character is")
print(chars)

print("Checking if any of the characters is present in the string")
print(any((c in chars) for c in str1))

Output

The output of the following program is 

The given string is
Welcome to Tutorialspoint
The given character is
{'5', ',', '6', '2', '0', '3', '$', '1', '7', '8', '9', '4'}
Checking if any of the characters is present in the string
False

Updated on: 07-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements