ascii_letters in Python Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report ascii_letters constant in Python is part of the string module and is a predefined string that contains all the lowercase and uppercase ASCII letters. It includes:Lowercase letters: abcdefghijklmnopqrstuvwxyzUppercase letters: ABCDEFGHIJKLMNOPQRSTUVWXYZIt is equivalent to the concatenation of string.ascii_lowercase and string.ascii_uppercase.Example: Python import string # Accessing ascii_letters s = string.ascii_letters print(s) OutputabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ Explanationascii_letters constant combines ascii_lowercase and ascii_uppercase into a single string.It contains all lowercase ('a' to 'z') and uppercase ('A' to 'Z') ASCII characters in sequence.Syntax of ascii_lettersstring.ascii_lettersParametersThis constant does not take any parameters as it is predefined in the string module.Return TypeReturns a string containing all ASCII alphabetic characters, both lowercase and uppercase.Example 1: Using 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: Python import string s = "Hello123" # Checking if all characters in a string are ASCII letters res = all(char in string.ascii_letters for char in s) print(res) OutputFalse Explanation:The string s contains digits ('123'), which are not part of ascii_letters.The all() function checks each character, resulting in False since not all characters are ASCII letters.Example 2: Generating a random stringSometimes, we need to generate a random string of alphabetic characters for tasks like creating random identifiers. Using ascii_letters, this becomes straightforward: Python import string import random # Generating a random string of length 8 using ascii_letters random_string = ''.join(random.choices(string.ascii_letters, k=8)) print(random_string) OutputyeDgOvur Explanation:random.choices() selects 8 random characters from ascii_letters.The join() method combines these characters into a single string, resulting in a random string of alphabetic characters.Example 3: Counting ASCII letters in a stringWe may want to count how many ASCII letters are present in a given string. This is a common task when analyzing text data: Python import string s = "Python3.9 is Awesome!" # Counting the number of ASCII letters in the string count = sum(1 for char in s if char in string.ascii_letters) print(count) Output15 Explanation:The generator expression iterates over each character in the string s.For each character that is in ascii_letters, the count is incremented, resulting in the total count of ASCII letters.Example 4: Separating letters from other charactersConsider a situation where we need to extract only the alphabetic characters from a mixed string. ascii_letters simplifies this task: Python import string s = "abc123XYZ!" # Extracting only ASCII letters from the string letters_only = ''.join(char for char in s if char in string.ascii_letters) print(letters_only) OutputabcXYZ Explanation:The generator expression filters out characters not in ascii_letters.The join() method combines the filtered characters into a new string containing only letters. Comment More infoAdvertise with us Next Article ord() function in Python A abhishek1 Follow Improve Article Tags : Python python-string ASCII Practice Tags : python Similar Reads ascii() in Python Python ascii() function returns a string containing a printable representation of an object and escapes the non-ASCII characters in the string using \x, \u or \U escapes. It's a built-in function that takes one argument and returns a string that represents the object using only ASCII characters. Exa 3 min read Python string - ascii_lowercase In Python, ascii_lowercase is a useful tool that gives us a string containing all the lowercase letters from 'a' to 'z'. We can use this for tasks like generating random strings or checking if a letter is lowercase. Example:Pythonfrom string import ascii_lowercase res = ascii_lowercase print(res)Out 2 min read Iterate over a set in Python The goal is to iterate over a set in Python. Since sets are unordered, the order of elements may vary each time you iterate. There are several ways to access and process each element of a set in Python, but the sequence may change with each execution. Let's explore different ways to iterate over a s 2 min read ord() function in Python Python ord() function returns the Unicode code of a given single character. It is a modern encoding standard that aims to represent every character in every language.Unicode includes:ASCII characters (first 128 code points)Emojis, currency symbols, accented characters, etc.For example, unicode of 'A 2 min read unicode_literals in Python Unicode is also called Universal Character set. ASCII uses 8 bits(1 byte) to represents a character and can have a maximum of 256 (2^8) distinct combinations. The issue with the ASCII is that it can only support the English language but what if we want to use another language like Hindi, Russian, Ch 3 min read as Keyword - Python as keyword in Python plays a important role in simplifying code, making it more readable and avoiding potential naming conflicts. It is mainly used to create aliases for modules, exceptions and file operations. This powerful feature reduces verbosity, helps in naming clarity and can be essential whe 3 min read Like