Python Forum
Strings containing both symbols and letters
Thread Rating:
  • 2 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strings containing both symbols and letters
#1
Hello, I am a student and i need to know how to check if a string contains both symbols and text. Any help would be greatly appreciated.
Reply
#2
What are "symbols"? Have you got a list?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
(Apr-04-2017, 07:35 PM)Ofnuts Wrote: What are "symbols"? Have you got a list?
The symbols on your keyboard e.g !"£$%^&*()_-+=

(Apr-04-2017, 07:36 PM)gullidog Wrote:
(Apr-04-2017, 07:35 PM)Ofnuts Wrote: What are "symbols"? Have you got a list?
The symbols on your keyboard e.g !"£$%^&*()_-+=
Any help would be greatly appreciated.
Reply
#4
If numbers are considered 'symbols',  you can use "str.isalpha()", if they are considered 'text', use "str.isalnum()"

examples:

alpha_str = "abcdef"
print(alpha_str.isalpha())

combo_str = "abcdef123"
print(combo_str.isalnum())

bad_str = "!@#$%^&*( )_+"
print(bad_str.isalnum())
output:

Output:
C:\Python36\python.exe C:/Python/Sound/scratch2.py True True False Process finished with exit code 0
Refer to String Operations and Built-in Types
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
(Apr-04-2017, 08:38 PM)sparkz_alot Wrote: *
Thanks, but what do you mean are they considered "text"?
Reply
#6
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:
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.
Reply
#7
Thanks, in the second way how does python understand what symbols to check for?
Reply
#8
I have run everything within one session of an interactive shell, so variables defined in first snippet were visible in following ones. If you want to try it separately, you need to define symbols and test_string before your code.
Reply
#9
(Apr-04-2017, 09:00 PM)zivoni Wrote: 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)
    ...:
The first one outputs true even if it has not got a symbol in
Reply
#10
For what test_string and symbols?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python script that deletes symbols in plain text nzcan 3 642 Sep-05-2023, 04:03 PM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 699 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,704 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  cyrillic symbols in tables in reportlab. hiroz 5 11,206 Sep-10-2020, 04:57 AM
Last Post: bradmalcom
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,012 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  Replacing symbols by " Tiihu 1 1,813 Feb-13-2020, 09:27 PM
Last Post: Larz60+
  How do I delete symbols in a list of strings? Than999 1 2,242 Nov-16-2019, 09:37 PM
Last Post: ibreeden
  Finding multiple strings between the two same strings Slither 1 2,482 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,183 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  Python symbols AND letters gullidog 1 3,464 Apr-05-2017, 10:13 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020