There are multiple ways how to check it. Some examples:
You can for each symbol test whether its in a string and stop when you found one or everything is tested:
You can for each symbol test whether its in a string and stop when you found one or everything is tested:
Output:In [32]: symbols = "!@#$%^&*()_+"
...: test_string = "string with $ symbol"
...:
...: contains_symbol = False
...: for symbol in symbols:
...: if symbol in test_string:
...: contains_symbol = True
...: break
...: print(contains_symbol)
...:
True
Last code could be rewritten as a list comprehension or generator expression:Output:In [33]: contains_symbol = any(symbol in test_string for symbol in symbols)
...: print(contains_symbol)
...:
True
You can convert both test_string and symbols to sets and check their intersection:Output:In [34]: common_symbols = set(symbols) & set(test_string)
...: print(common_symbols)
...: contains_symbol = bool(common_symbols)
...: print(contains_symbol)
...:
{'$'}
True
You can import re and use regular expression to search for occurence:Output:In [38]: import re
...: re.search("[!@#$%^&*\(\)_+]", test_string)
...:
Out[38]: <_sre.SRE_Match object; span=(12, 13), match='$'>
To check if string contains text character you would do exactly same but instead of symbols string you would use string consisting of your "text" characters. Its possbile to check both symbols and text with one regular expression, but matching pattern for that will be little more complicated.