
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
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
- Related Articles
- How to remove specific characters from a string in Python?
- Search for specific characters within a string with MySQL?
- How to scan for a string in multiple document formats (CSV, Text, MS Word) with Python?
- How to scan through a directory recursively in Python?
- MySQL query to select a specific string with special characters
- How to remove a list of characters in string in Python?
- How to parse for words in a string for a specific word in java?
- How to search a MySQL table for a specific string?
- Set characters at a specific position within the string in Arduino
- Python Program to find mirror characters in a string
- Python Program to Capitalize repeated characters in a string
- How to check if a string only contains certain characters in Python?
- How to trim down non printable characters from a string in Python?
- How to remove characters except digits from string in Python?
- Find if a string begins with a specific set of characters in Arduino
