How to Escape Special Characters of a Python String with a Single Backslash?

The backslash escape character ‘\’ is a special Python string character that is usually followed by an alphabetic character. For example, the tabular whitespace ‘\t’ and newline ‘\n’. In regular expressions, you can use the single escape to remove the special meaning of regex symbols. For example, to match the dot or asterisk characters ‘.’ … Read more

Python Regex Split Without Empty String

Problem Formulation Say, you use the re.split(pattern, string) function to split a string on all occurrences of a given pattern. If the pattern appears at the beginning or the end of the string, the resulting split list will contain empty strings. How to get rid of the empty strings automatically? Here’s an example: Note the … Read more

Python endswith() Tutorial – Can We Use Regular Expressions?

While refactoring my Python code, I thought of the following question. Can You Use a Regular Expression with the Python endswith() Method? The simple answer is no because if you can use a regex, you won’t even need endswith()! Instead, use the re.match(regex, string) function from the re module. For example, re.match(“^.*(coffee|cafe)$”, tweet) checks whether … Read more

Check if All Characters of a String are Uppercase

Problem Formulation: How to check if all characters of a string are uppercase? Background: A string is a sequence of characters, and is amongst the most commonly used and popular data types in Python. Strings can be enclosed by either single or double quotes and are β€˜immutable’, meaning they can’t be changed once created. There … Read more

How To Extract All Emojis From Text in Python?

Summary: This blog explains the various ways one can extract commonly used Emojis embedded within text. Note: All the solutions provided below have been verified using Python 3.9.0b5. Problem Formulation One has a list with normal text words and emojis, all mixed together, as shown below.  How does one extract only the emojis, into a … Read more

How to Split a String Between Numbers and Letters?

Problem Formulation: Given a string of letters and numbers. How to split the string into substrings of either letters or numbers by using the boundary between a letter and a number and vice versa. Examples: Have a look at the following examples of what you want to accomplish. ‘111A222B333C’ —> [‘111’, ‘A’, ‘222’, ‘B’, ‘333’, … Read more

Regex Special Characters – Examples in Python Re

Regular expressions are a strange animal. Many students find them difficult to understand – do you? I realized that a major reason for this is simply that they don’t understand the special regex characters. To put it differently: understand the special characters and everything else in the regex space will come much easier to you. … Read more

Python Re Escape

If you’re like me, you’ll regularly sit in front of your code and wonder: how to escape a given character? Challenge: Some characters have a special meaning in Python strings and regular expressions. Say you want to to search for string “(s)” but the regex engine takes the three characters (s) as a matching group. … Read more

Python Regex Finditer()

You can create an iterable of all pattern matches in a text by using the re.finditer(pattern, text) method: Specification: re.finditer(pattern, text, flags=0) Definition: returns an iterator that goes over all non-overlapping matches of the pattern in the text. The flags argument allows you to customize some advanced properties of the regex engine such as whether … Read more