Checking If Any Character In A String Is Alphanumeric
I want to check if any character in a string is alphanumeric. I wrote the following code for that and it's working fine: s = input() temp = any(i.isalnum() for i in s) print(temp
Solution 1:
In your second function you apply any
to a single element and not to the whole list. Thus, you get a single bool element if character i
is alphanumeric.
In the second case you cannot really use any
as you work with single elements. Instead you could write:
for i in s:
if i.isalnum():
print(True)
break
Which will be more similar to your first case.
Solution 2:
any()
expects an iterable. This would be sufficient:
isalnum = Falsefor i in s:
if i.isalnum():
isalnum = Truebreakprint(isalnum)
Post a Comment for "Checking If Any Character In A String Is Alphanumeric"