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.
Strings containing both symbols and letters
Strings containing both symbols and letters
|
Apr-04-2017, 07:35 PM
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 (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:Any help would be greatly appreciated.(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, 08:38 PM
(This post was last modified: Apr-04-2017, 08:38 PM by sparkz_alot.)
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: 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
Apr-04-2017, 08:41 PM
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: Last code could be rewritten as a list comprehension or generator expression: You can convert both test_string and symbols to sets and check their intersection: You can import re and use regular expression to search for occurence: 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.
Thanks, in the second way how does python understand what symbols to check for?
Apr-04-2017, 09:00 PM
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.
Apr-04-2017, 09:20 PM
(Apr-04-2017, 09:00 PM)zivoni Wrote: symbols = "!@#$%^&*()_+"The first one outputs true even if it has not got a symbol in
Apr-04-2017, 09:24 PM
For what test_string and symbols?
|
|
Users browsing this thread: 1 Guest(s)