Test String Contents

Thomas Nyberg tomuxiong at gmx.com
Tue Jun 13 18:49:20 EDT 2017


On 06/13/2017 03:34 PM, Matt wrote:
> What is easiest way to determine if a string ONLY contains a-z upper
> or lowercase.  I also want to allow the "-" and "_" symbols.  No
> spaces or anything else.
> 
I'm not sure it's the best way, but the following seems okay:

>>> s = 'hello_world'
>>> s.replace('-','').replace('_','').isalpha()
True
>>> s = 'hello world'
>>> s.replace('-','').replace('_','').isalpha()
False
>>>

Cheers,
Thomas



More information about the Python-list mailing list