Found 33676 Articles for Programming

How to check if a character is upper-case in Python?

Sarika Singh
Updated on 09-Jun-2025 08:55:59

42K+ Views

In this article, we are going to find out how to check if a character is an uppercase letter in Python. We will explore four different approaches - Using the isupper() method Using regular expressions Using ASCII values Direct comparison In each approach, we will check whether a given character is in uppercase and return True or False accordingly. Using the isupper() Method The first and most straightforward way is by using the built-in isupper() method. This method returns True if the ... Read More

How to check if text is “empty” (spaces, tabs, newlines) in Python?

Sarika Singh
Updated on 11-Jun-2025 15:38:27

4K+ Views

In this article, we are going to focus on how to check if the text is empty (spaces, tabs, newlines) in Python. Checking if text is Empty using isspace() method The isspace() method determines whether a string contains only spaces or contains any other characters. If the given string is only made up of spaces, this method returns true; otherwise, it returns false. Even if the string contains characters like t and n, the method returns true. Example In the program given below, we are taking 3 different strings and finding out if they contain only spaces using the isspace() ... Read More

How to check if a string contains only whitespace letters in Python?

Sarika Singh
Updated on 09-Jun-2025 07:11:43

12K+ Views

A string is considered to contain only whitespace if it consists entirely of characters like space (' '), tab ('\t'), newline (''), carriage return ('\r'), etc. Python provides ways to check this directly using built-in functions or regular expressions. Using isspace() Method The isspace() method returns True if all characters in the string are whitespace characters and the string is not empty. If there is any non-whitespace character, it returns False. Example: Only Whitespace In the following example, the string contains a few spaces and tab characters, so the isspace() method returns True - str1 = " \t ... Read More

How to check if a string contains only upper case letters in Python?

Sarika Singh
Updated on 09-Jun-2025 07:44:48

9K+ Views

Checking for Uppercase Letters in Python A string is considered to contain only upper-case letters if all its characters are alphabets and each one is in upper case (from 'A' to 'Z'). Python provides various ways to check this, such as using the isupper() method, regular expressions, or manually checking each character. Using isupper() Method The isupper() method returns True if all the alphabetic characters in the string are uppercase and there is at least one alphabet. If the string contains any lowercase letters or non-alphabetic characters, it returns False. Example: All Uppercase Letters In the following example, the string ... Read More

How to check if a string contains only lower case letters in Python?

Sarika Singh
Updated on 09-Jun-2025 08:17:16

4K+ Views

Checking for Lowercase Letters in PythonIn Python, a string is considered to have only lower-case letters if all its characters are alphabets and each one of them is in lower case (from 'a' to 'z'). We can verify this using the built-in islower() method, regular expressions, or by checking each character using the all() method. Using islower() Method The islower() method returns True if all the characters in the string are in lowercase and there is at least one alphabet. If the string contains uppercase letters or non-alphabetic characters, it returns False. Example: All Lowercase Letters In the following example, ... Read More

How to check if a unicode string contains only numeric characters in Python?

Sarika Singh
Updated on 02-Sep-2025 13:21:31

877 Views

In Python, Unicode strings can contain numeric characters from various languages and scripts. To check if a Unicode string contains only numeric characters, we can use built-in string methods, regular expressions, or character iteration. These methods ensure that characters like Arabic numerals, Chinese digits, or superscripts are also recognized as valid numeric characters. Using isnumeric() Method The isnumeric() method returns True if all characters in the string are numeric, including Unicode numeric characters like ², ١, 一, etc. If any character is non-numeric or the string is empty, it returns False. Example: Unicode Numeric Characters In the following example, ... Read More

How to check if a string only contains certain characters in Python?

Sarika Singh
Updated on 02-Sep-2025 13:21:55

9K+ Views

To check if a Python string contains only certain characters, you can use - set Comparison Regular Expressions Character Lists These approaches help you to verify whether every character in the string belongs to a defined set of allowed characters or not. Using Set Comparison You can create a set of allowed characters and check if all characters in the string belong to that set or not. If the string is a subset of the allowed set, it means the string contains only valid (accepted) characters. Example In the following example, we define a set of allowed numeric ... Read More

How to check if a Python string contains only digits?

Sarika Singh
Updated on 02-Sep-2025 13:15:29

12K+ Views

To check if a Python string contains only digits, you can use the built-in isdigit() method. This method returns True if all characters in the string are digits, and False otherwise. Besides isdigit(), you can also use loops or regular expressions to verify this condition. Using the isdigit() Method The isdigit() method is used to check if a string contains only digits. It returns True only when every character is a digit. Example In this example, we define a string that contains only digits and use the isdigit() method to check it. The output confirms that the string contains only ... Read More

What is the Python regular expression to check if a string is alphanumeric?

Sarika Singh
Updated on 02-Sep-2025 13:22:21

6K+ Views

In this article, we focus on how to check if a string is alphanumeric using regular expressions in Python. Regular expressions are very useful for pattern matching and validation. To use them, first import the re library, which is included by default in Python. The regular expression ^[a-zA-Z0-9]+$ matches strings that contain only letters (both uppercase and lowercase) and numbers. This expression returns True if the string is alphanumeric; otherwise, it returns False. Using Regular Expressions By applying the re.match() function with the above pattern, you can check if the entire string contains only alphanumeric characters. Example: Checking an Alphanumeric ... Read More

How to check if a string is alphanumeric in Python?

Sarika Singh
Updated on 09-Jun-2025 09:19:01

7K+ Views

In Python, strings can contain letters, numbers, or special characters. To check if a string is alphanumeric (contains only letters and numbers), we can use different methods. This article shows three simple ways to do that: Using the isalnum() function Using regular expressions Using the isalpha() and isdigit() functions Using the isalnum() Function The isalnum() method returns True if all characters in the string are letters or digits; otherwise, it returns False. Example: Basic Alphanumeric Check In the example below, we take two strings and check if they contain only alphabets and numbers using the isalnum() function: str1 ... Read More

Advertisements