Characters are signs or symbols – such as letters, numbers and currency signs – that convey information. They are such an integral part of programming that some languages, like C, even make them an explicit type of data for storing a single character value.
However, Python is not one of these languages. Instead, the Python wire Data type folds zero or more characters as a single object.
Still, we understand that different characters can represent different types of information and therefore need to be categorized differently. For example, while “a” and “1” are strings, the first is an alphabetic character while the second is numeric. This might be an important distinction in your program, though Python would consider them the same type of data (class ‘str’). Fortunately, there is a way to distinguish between them!
In this article you will learn eight different ones String methods That you can use it to check exactly what Type Of character data are contained within a given string.
This character classification method returns True if a string contains characters classified as a Letter In the Unicode Character Database, And False otherwise:
>>> letters_only = "InitialCommit" >>> letters_only.isalpha() True
If the string contains a character other than a letter, such as a white space character, the result will be false:
>>> letters_and_spaces = "Initial Commit" >>> letters_and_spaces.isalpha() False
Note that you can also call these classification methods directly on the string itself, without assigning it to a variable first:
>>> "InitialCommit".isalpha() True
This Convention will be used later in the article.
This character classification method returns true if a character is an integer in the Base-10 number system:
>>> "0".isdecimal() True
Note that this classification method does this No Work for what you might intuitively think of as a decimal number, that is, a positive or negative number that may or may not include a decimal point:
>>> "3.14159".isdecimal() False >>> "-1".isdecimal() False
In other words, this method returns true only for the set of positive integers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (including zero).
This character classification method returns true if a symbol is an expression of the ten decimal numbers. It offers support for Unicode subscribers, overwriters, and more:
>>> "u00B2".isdigit() # superscript 2 True
This classification does not yet work for negative numbers:
>>> "-1".isdigit() False
As the minus operator (
- ) Is technically not a number itself, it is expected.
This character classification method returns True if a conceptual symbol can be interpreted as a number, even if the decimal digits themselves are not used. It adds support for Roman numerals, fractions, coin counters and much more:
>>> "u2168".isnumeric() # roman numeral for the number 9 True
Again, strings for negative numbers due to the inclusion of the minus sign operator are not counted as libraries in Python:
>>> "-1".isnumeric() False
This character classification method returns True if one of the previous four conditions is true:
>>> "abcdefg".isalnum() # letters: isalpha True >>> "12345".isalnum() # numbers: isdecimal True >>> "u00B2".isalnum() # superscript 2: isdigit True >>> "u2168".isalnum() # roman numeral 9: isnumeric True
This character classification method returns true if the string represents a White space Character (space, tab or new row included
>>> " ".isspace() # four spaces True >>> " ".isspace() # a tab True >>> """ ... ... """.isspace() # a multiline string True
You can also use this method in escape sequences:
>>> "n".isspace() # newline True >>> "t".isspace() # tab True >>> "r".isspace() # carriage return True >>> "f".isspace() # form feed True
This character classification method returns true if all The characters in the string are printable. In general, control characters such as those seen in the previous section indicating new rows, cart returns, separators, etc. will not be considered printable:
>>> "n".isprintable() # alphabet True >>> "n".isprintable() # newline False >>> "t".isprintable() # tab False >>> "r".isprintable() # carriage return False
Bonus: string.ididentifier ()
This character classification method returns true if a string is a valid name for a Python object, whether it is a variable, function, class, module, etc. The string must follow Python’s rules for naming objects; In other words, it must start with a letter, and can contain any decimal number 0-9 or underscores:
>>> "hello".isidentifier() True >>> "1hello".isidentifier() False >>> "h_e_l_l_o".isidentifier() True
However, this method cannot check whether it is a desirable ID or not Saved keyword. These are words, like
else Used by Python to control program flow and not available for object assignment:
>>> "and".isidentifier() True
In this case, the string
"and" Set as a valid identifier, even though it will not be allowed in Python due to its status as a reserved keyword. Attempting to assign a Python object to this word will result in a
>>> and = "my new string" File "<stdin>", line 1 and = "my new string" ^ SyntaxError: invalid syntax
When using this string method, you will need to take another step to ensure that the selected string is not a cached keyword, in addition to being a valid identifier. You can do this by calling
iskeyword() To function from the built-in
>>> from keyword import iskeyword >>> iskeyword('and') True
You can then rewrite your code to ensure that the result of
str.isidentifier() True while the result of
iskeyword() Is a lie:
>>> "hello".isidentifier() True >>> iskeyword("hello") False
Thus, you can confirm that the desired string is indeed available for use as an object identifier.
In this article, you learned how to use string methods to identify what type of character data is included in a given string.
You have seen various methods for classifying common symbols in the form of letters and numbers, as well as methods for working with special characters such as Roman numerals, subtitles and super-super, space characters and more.
Finally, you saw how you can use
str.isidentifier() A method combined with the built-in
iskeyword() Function to ensure that a given string is indeed available for use as a Python identifier.
In this article you have seen three different ways to design strings in Python. There are other actions you can perform on strings as well, including turning strings into lowercase letters and checking if a string contains a sub-string.
If you are interested in learning more about the basics of Python, coding and software development, see the Developer Coding Basics Guide, where we cover the essential languages, concepts and tools you will need to become a professional developer.
Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, please feel free to contact firstname.lastname@example.org.