Found 10402 Articles for Python

How to replace all occurrences of a string with another string in Python?

Sarika Singh
Updated on 11-Jun-2025 16:49:42

2K+ Views

In Python, you can replace all occurrences of a substring within a string using the replace() method. This method returns a new string where every occurrence of the given old substring is replaced with a new substring. To match the string with case-insensitivity, you need to use regular expressions. Using the replace() Method The Python replace() method returns a new string where all occurrences of the old substring are replaced with the new substring. It does not modify the original string because strings in Python are immutable. string.replace(oldvalue, newvalue, count) where, ... Read More

How to remove all leading whitespace in string in Python?

Sarika Singh
Updated on 11-Jun-2025 17:16:16

17K+ Views

Leading whitespace refers to any spaces, tabs, or other blank characters that appear at the start of a string. These characters come before the first visible text and can affect how the string is displayed. We can remove all leading whitespace from a string in Python using the lstrip() function. Using the lstrip() Function The lstrip() function removes all whitespace characters from the beginning (left side) of a string (leading whitespace). It does not affect any whitespace at the end or in the middle of the string. There are also two related functions i.e. rstrip() and strip() - ... Read More

How to convert all uppercase letters in string to lowercase in Python?

Sarika Singh
Updated on 11-Jun-2025 17:08:38

4K+ Views

We can convert all uppercase letters in a string to lowercase in Python using the lower() function. You can also use a for loop or list comprehension to change each character to lowercase one by one. This is helpful when you want to compare strings without worrying about letter cases. Using lower() Function The Python lower() function returns a new string with all uppercase letters converted to lowercase, keeping the lowercase letters unchanged. It does not modify the original string and is commonly used when comparing strings or handling user input. Example In the following example, we convert an entire string ... Read More

How to get the length of a string in Python?

Sarika Singh
Updated on 09-Jun-2025 07:25:27

3K+ Views

To find how many characters are present in a string (i.e. length of a string), Python provides the built-in function named len(). The length includes all characters in the string, including spaces, punctuation, and special characters. Using len() Function The len() function is the most direct way to find out how many characters are in a string. It returns the total number of characters, including spaces and special symbols. Example In the following example, we are calculating the length of a string using the len() function - text = "Python Programming" length = len(text) print("Length of the string is:", length) ... Read More

How to join two strings to convert to a single string in Python?

Sarika Singh
Updated on 09-Jun-2025 09:06:16

2K+ Views

In Python, we can join two strings into one single string using different ways, such as - Using the + Operator Using the join() Method Using Formatted Strings String Concatenation in Loops Let us look at each method with examples. Using + Operator The most common way to combine two strings into one in Python is by using the + operator. It joins the strings exactly as they are, without any space or separator in between. Example In the following example, we ... Read More

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

Advertisements