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 ascii_letters in Python A abhishek1 Follow Improve Article Tags : Python python-string ASCII Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is 8 min read ASCII Values Alphabets ( A-Z, a-z & Special Character Table ) ASCII (American Standard Code for Information Interchange) is a standard character encoding used in telecommunication. The ASCII pronounced 'ask-ee', is strictly a seven-bit code based on the English alphabet. ASCII codes are used to represent alphanumeric data. The code was first published as a sta 7 min read Like