

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to scan a string for specific characters in Python?
If you want to check if a given character exists in a string, you can use in. For example,
>>> s = "Hello world" >>> 'e' in s True
If you have a list of characters you want to search, you can use Sets. Add these character in the set and use the any function to check if any of these characters exist in the string. For example,
from sets import Set chars = Set('0123456789$,') s = "I have 9 cats" if any((c in chars) for c in s): print('Found') else: print('Not Found')
This will give the output:
Found
If you want to check if all of these characters exist in the string, just replace any with all. For example,
from sets import Set chars = Set('0123456789$,') s = "I have 9 cats" if all((c in chars) for c in s): print('Found') else: print('Not Found')
This will give the output:
Not Found
- Related Questions & Answers
- 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 search a MySQL table for a specific string?
- How to parse for words in a string for a specific word in java?
- Set characters at a specific position within the string in Arduino
- How to remove a list of characters in string in Python?
- Queries for characters in a repeated string in C++
- Python Program to find mirror characters in a string
- Find if a string begins with a specific set of characters in Arduino
- How to Replace characters in a Golang string?
- C++ Program to Generate a Sequence of N Characters for a Given Specific Case
- How to check if a string only contains certain characters in Python?
Advertisements