

Testing membership and empty strings
source link: https://pybit.es/articles/testing-membership-and-empty-strings/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Testing membership and empty strings
By J. Ryan Hall on 20 January 2023
I was working on one of the exercises on the Pybites platform (Bite 29) and encountered a situation I didn’t understand. I needed to check a set of inputs to see if they were alphanumeric or not as part of the solution to the exercise. I succeeded in all but one test, but I couldn’t tell why one failed so I researched why that was and would like to share it.
Note – if you haven’t completed the Bite, the solution might be partially spoiled below so maybe you want to do the exercise before reading on.
Membership testing is a core function of Python, but sometimes what we might expect isn’t what we get, and it’s important to understand why that is. Let’s take a look at the membership test operations in the context of characters and strings.
The membership test operations (as the name implies) test for the membership of an element within another element, and evaluate to either true or false. For example, consider the expression x in s
. It returns True
if x is present in s, otherwise it returns False
.
I was testing whether characters char
were alphanumeric by checking for their membership in a string of all the alphanumeric characters alpha
(so, a-z+A-Z+0-9
) and encountered the issue that – although I didn’t intend for it to – an empty string’s membership was evaluating to True
.
>>> char = ''
>>> alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
>>> alpha
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
>>> char in alpha
True
This is because the membership test is checking if the character char
is a substring of alpha
, and according to the documentation an empty string is always considered to be a substring of any other string.
Instead, what I intended to test was the membership of an element char
against a collection of individual elements considered alphanumeric, and for that, Python has a number of solutions. For my case though, I would like to use the set given in the exercise. The solution* is to list-ify that string of characters, then test for the membership of char
against that list of elements, like so:
>>> char = ''
>>> alpha = list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
>>> alpha
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B',
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9']
>>> char in alpha
False
*Editor’s note: Sets are more performant than lists for lookups because they support hashing. If you’re interested in reading more about this, you can start here.
I still have much to learn in Python, but the Pybites platform has already been a great educational resource for me, and I encourage anyone who is interested in developing their Python skills to try it out!
</div
Recommend
-
13
Membership proofs and De Bruijn indicesMembership proofs and De Bruijn indices 2020, Jan 19 by Tesla Ice Zhang {-#
-
8
Testing Google Search On Gecko With Different UA Stringsotsukare Thoughts after a day of workGoogle is serving very different versions of its services to individual browsers and device...
-
17
Differences Between #nil?, #empty?, #blank?, and #present? Joyce Echessa on Sep 11, 2018 “I absolutely love AppSignal.” Discover AppSi...
-
10
Strings Contain Empty Strings This is just one of those philosophical moments in programming where one small details can have a big impact. In C# / .NET you will find that when you ask if a string contains an empty string (
-
7
Conversation Copy link Contributor...
-
8
[Golang] Remove Leading and Trailing Empty Strings in String Array J...
-
5
Why unions are growing and shrinking at the same time Joining the picket line like it’s 1939. By
-
5
Python's "in" and "not in" OperatorsGetting Started With Membership Tests in Python Sometimes you need to find out whether a value is present in a collection of...
-
15
This tutorial will discuss about unique ways to check if numpy array contains only empty strings. Table Of Contents Method 1: using numpy.char.str_len() The numpy
-
10
Check if Array Contains Only Empty Strings in C++ This tutorial will discuss about a unique way to check if array contains only empty strings in C++. Suppose we have a string array. Like this,
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK