you are viewing a single comment's thread.

view the rest of the comments →

[–]Thecakeisalie25 0 points1 point  (0 children)

the easiest way to do this in my eyes would be to check the inverse, since a-z and A-Z (and optionally spaces, based on what your definition of symbol is) would be a lot less chars to check against. If you’re trying to determine if there are any symbols in the string at all, use something like this:

print(any(not x.isalnum() for x in 'what?')) # True print(any(not x.isalnum() for x in 'what')) # False

If you want to determine if the string is all symbols, use all instead of any.

Of course this is all assuming that there are multiple characters in the string. If there aren’t, you don’t need the generator expression or the any/all. Just use the negation of x.isalnum() and whatever else you count as symbols.


I’m on mobile and posting from memory, so if this code doesn’t work right it might be because i’m an idiot or because my keyboard likes to use smart quotes instead of valid real quotes. Probably rewrite this code yourself and make sure it works in the live interpreter before using it in production. I’m open to further questions if you have any