In this lesson, you’ll explore string methods that classify a string based on the characters it contains. Here are character classification methods:
str.isalnum()
str.isalpha()
str.isdigit()
str.isidentifier()
*iskeyword(<str>)
*str.isprintable()
str.isspace()
str.istitle()
str.islower()
str.isupper()
str.isascii()
*
The method .isidentifier()
determines whether the target string is a valid identifier. What is a Python identifier? An identifier is a name used to define a variable, function, class, or some other type of object.
Python identifiers:
- Must begin with an alphabetic character or underscore (
_
) - Can be a single character
- Can be followed by any alphanumeric or the underscore
- Cannot have other punctuation characters
One other potential pitfall to watch for in naming your own identifiers is that you can’t use an existing Python keyword. To check if the name you’re considering using is an existing keyword, you can import a function called iskeyword()
from the module called keyword
:
>>> from keyword import iskeyword
>>> iskeyword('and')
True
Here are Python keywords:
Python Keywords | |||||
---|---|---|---|---|---|
False |
break |
else |
if |
not |
while |
True |
class |
except |
import |
or |
with |
None |
continue |
finally |
in |
pass |
yield |
and |
def |
for |
is |
raise |
global |
as |
del |
from |
lambda |
return |
nonlocal |
assert |
elif |
try |
For more information on Python modules, check out
Python Modules and Packages — An Introduction . The .isascii()
method was introduced in Python 3.7.
Here’s how to use str.isalnum()
:
>>> s = 'abc123'
>>> s.isalnum()
True
>>> ''.isalnum()
False
>>> s = 'abc$123'
>>> s.isalnum()
False
Here’s how to use str.isalpha()
:
>>> s = 'ABCabc'
>>> s.isalpha()
True
>>> s = 'ABC123'
>>> s.isalpha()
False
Here’s how to use str.isdigit()
:
>>> s = '123456'
>>> s.isdigit()
True
>>> s = '123abc'
>>> s.isdigit()
False
Here’s how to use str.isidentifier()
:
>>> 'spam32'.isidentifier()
True
>>> '32spam'.isidentifier()
False
>>> 'foo$32'.isidentifier()
False
>>> 'def'.identifier()
True
Here’s how to use iskeyword()
:
>>> from keyword import iskeyword
>>> 'def'.isidentifier()
True
>>> 'def'.iskeyword()
Traceback (most recent call last):
File "<input>", line 1, in <module>
'def'.iskeyword()
AttributeError: 'str' object has no attribute 'iskeyword'
>>> iskeyword('def')
True
>>> iskeyword('and')
True
>>> iskeyword('spam32')
False
Here’s how to use str.isprintable()
:
>>> s = 'a\tb'
>>> s.isprintable()
False
>>> s = 'a b'
>>> s.isprintable()
True
>>> s = ''
>>> s.isprintable()
True
>>> s = 'a \n b'
>>> s.isprintable()
False
Here’s how to use str.isspace()
:
>>> s = 'a \n b'
>>> s.isspace()
False
>>> s = '\t\n '
>>> s.isspace()
True
Here’s how to use str.istitle()
:
>>> s = 'The Sun Also Rises'
>>> s.istitle()
True
>>> s = "Bob's Burgers!"
>>> s.istitle()
False
Here’s how to use str.islower()
:
>>> s = 'asdlkjgadb'
>>> s.islower()
True
>>> s = "spamIsgood"
>>> s.islower()
False
Here’s how to use str.isupper()
:
>>> s = 'SPAMBACON'
>>> s.isupper()
True
>>> s = "SPAMBACON#1!"
>>> s.isupper()
True
Here’s how to use str.isascii()
:
>>> s = 'ABCabc#$%'
>>> s.isascii()
True
>>> ''.isascii()
True
>>> ' '.isascii()
True
>>> '∑'.isascii()
False