Python Articles

Page 43 of 852

How to check if multiple strings exist in another string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 21-Apr-2025 11K+ Views

While working with the strings, Checking if the string exists inside another string is a common task and can be achieved by using the in keyword. But, In this article we are going to see how to check if multiple strings exist in another string in Python. For example, checking the multiple strings: "TP", "WELCOME", exists in the string "WELCOME to TP". For achieving this, Python provides different methods including loops, any() and all(). Using Python any() Function The Python any() function returns true if any of the element of the given iterable (such as list, ...

Read More

What is the most elegant way to check if the string is empty in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 21-Apr-2025 4K+ Views

The strings are the fundamental data types that are used to store text. When working with the string, Checking if the string is empty or not is a common task. An empty string is nothing but the string with zero characters. There are various to check if a sting is empty, But choosing the elegant ways helps to improve the code readability. Let's dive into the article to learn more about it. Using if not my_string In Python, empty strings are considered as falsy, Which evaluates to false in a boolean content. So using not my_string returns true ...

Read More

How to remove empty strings from a list of strings in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 21-Apr-2025 9K+ Views

The Lists in Python are used to store collections of items such as numbers, strings. While working with the list of strings, it is common to find a empty strings in the list. An empty string is nothing but a string value with zero length. Officially they are present as elements in the list, but does not hold any data and interfere in the operations like sorting, filtering. So, removing the empty strings is essential to ensure the list contains only valid values. For achieving this, Python provides the several ways. Let's dive into the article to learn ...

Read More

How to process escape sequences in a string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 21-Apr-2025 3K+ Views

The escape sequences are the special characters that are used to represent the whitespace characters, quotes, backslashes or non-printable characters within the string. These sequences start with the backslash followed by the character, For example result in newline. However, in some cases we will come across the situations to process or even to ignore these situations. In this article we will learn how to process the escape sequences. Example Let's look at the following example, where we are going to consider the basic approach to process the escape sequences. str1 = "Hello\tEveryoneWelcome to TutorialsPoint" print(str1) ...

Read More

How to strip spaces/tabs/newlines using Python regular expression?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 21-Apr-2025 980 Views

Python regular expressions (regex) provide various ways to handle whitespaces, including spaces, tabs, and newline characters, which can be effectively stripped from strings using regex. This article will explain how to split a string on newline characters using different regular expressions, following are the various methods to achieve the present task. Using re.split(r"[]", text) Splitting on One or More Newlines Using Quantifier [+] Splitting on Newlines with Whitespace Using re.split(r"\s*", text) Using re.split(r"[]", text) The re.split() function splits a string wherever the specified regular expression pattern ...

Read More

How to split on successions of newline characters using Python regular expression?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 21-Apr-2025 461 Views

Python's built-in splitlines() method and the split() method with as a delimiter are sufficient to split strings based on newline characters. This article will explore different approaches to splitting strings on sequences of newline characters using Python's regular expressions. Splitting on One or More Newlines The Python re.split() function uses a regular expression to split a string. We'll use the pattern +, which means one or more newlines. The re.split() will find where these newlines are, split the string there, and return a list of the resulting pieces. The re.split() function then splits the string at each ...

Read More

How can I find all matches to a regular expression in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 21-Apr-2025 289 Views

To find all matches to a regular expression in Python, you can use the re module, which provides regular expression matching operations. Here are a few methods to find all matches to regular expressions. Using re.findall(pattern, string) Using re.finditer(pattern, string) re.compile combined with findall or finditer Using re.findall(pattern, string) The re.findall() method finds all non-overlapping matches of the pattern in the string and returns them as a list of strings. This method returns a list of strings. If the pattern contains capturing groups, it returns ...

Read More

How can I append a tuple into another tuple in Python?

Sindhura Repala
Sindhura Repala
Updated on 20-Apr-2025 12K+ Views

In this article, we will demonstrate how to append one tuple to another in Python. Below are various methods to achieve this task - Using + operator. Using sum() function. Using list() & extend() functions. ...

Read More

What is function of ^ operator in Python

Akshitha Mote
Akshitha Mote
Updated on 18-Apr-2025 1K+ Views

In Python, the XOR operator is one of the bitwise operators and is represented by the caret symbol ^. It returns 0 if both operands are the same and 1 if the operands are different. Truth Table of XOR The following is the truth table of the XOR (exclusive OR) operator - A B ...

Read More

Does Python have a ternary conditional operator?

Akshitha Mote
Akshitha Mote
Updated on 18-Apr-2025 385 Views

The Python ternary operator returns a value based on whether a condition is True or False. It is similar to an if-else statement but is expressed in a single line. For understanding the ternary operator, we need to have an idea about conditional statements. Let's have a basic understanding of the if-else statement. We will have an if block followed by an else block here. The if block is executed when the given condition is True,  and the else block is executed if the given condition is False. The following is the basic syntax of the if-else statement - if condition: ...

Read More
Showing 421–430 of 8,519 articles
« Prev 1 41 42 43 44 45 852 Next »
Advertisements