![]() |
VOOZH | about |
ascii_letters constant in Python is part of the stringmodule and is a predefined string that contains all the lowercase and uppercase ASCII letters. It includes:
It is equivalent to the concatenation of string.ascii_lowercase and string.ascii_uppercase.
Example:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
ascii_letters constant combines ascii_lowercase and ascii_uppercase into a single string.
string.ascii_letters
string module.ascii_letters for validationIf we want to validate whether all characters in a string are ASCII letters. Here's how we can achieve this using the ascii_letters constant:
False
s contains digits ('123'), which are not part of ascii_letters.all() function checks each character, resulting in False since not all characters are ASCII letters.Sometimes, we need to generate a random string of alphabetic characters for tasks like creating random identifiers. Using ascii_letters, this becomes straightforward:
yeDgOvur
random.choices() selects 8 random characters from ascii_letters.join() method combines these characters into a single string, resulting in a random string of alphabetic characters.We may want to count how many ASCII letters are present in a given string. This is a common task when analyzing text data:
15
s.ascii_letters, the count is incremented, resulting in the total count of ASCII letters.Consider a situation where we need to extract only the alphabetic characters from a mixed string. ascii_letters simplifies this task:
abcXYZ
ascii_letters.join() method combines the filtered characters into a new string containing only letters.