Python Re * – The Asterisk Quantifier for Regular Expressions

Every computer scientist knows the asterisk quantifier of regular expressions. But many non-techies know it, too. Each time you search for a text file *.txt on your computer, you use the asterisk operator. But how does it work? This article is all about the asterisk * quantifier in Python’s re library. Study it carefully and master … Read more

56 Python One-Liners to Impress Your Friends

This is a running document in which I’ll answer all questions regarding the single line of Python code. It’s based on my interactive collection here but without the slow videos and embedded code shells. Let’s get started! Python One Line If Else You can use a simple if statement in a single line of code. … Read more

Regex to Match Dollar Amounts with Optional Cents

Problem: How to match dollar amounts in a given string? If cent amounts are given, those should match as well. Solution: You can use regular expression pattern ‘(\$[0-9]+(.[0-9]+)?)’ to find all numbers that start with a dollar symbol, followed by an arbitrary number of numerical digits, followed by an optional decimal point and an arbitrary … Read more

How To Split A String And Keep The Separators?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Problem Formulation πŸ”Problem: Given a string in Python; how to split the string and also keep the separators/delimiter? A sequence of one or more characters used to separate two or more parts of a given string or a … Read more

What is the Asterisk / Star Operator (*) in Python?

Asterisk Operator

Many Python coders—even at intermediate skill levels—are often puzzled when it comes to the asterisk character in Python. What does it mean? How does it work? What’s it purpose? This article answers all of those questions and more. After studying this article, you will have a solid understanding of the asterisk operator * in Python—and … Read more

How to Search and Replace a Line in a File in Python? 5 Simple Ways

Problem: Given the contents of a text file. How to search and replace a specific string or line in the file? Example: Let’s consider the following example where you want to replace the highlighted (bolded) text parts. Text in the file before replacing a line: There was an idea to bring together a group of … Read more

Python One Line Regex Match

Summary: To match a pattern in a given text using only a single line of Python code, use the one-liner import re; print(re.findall(pattern, text)) that imports the regular expression library re and prints the result of the findall() function to the shell. Problem: Given a string and a regular expression pattern. Match the string for … Read more

Python One Line X

This is a running document in which I’ll answer all questions regarding the single line of Python code. If you want to become a one-liner wizard, check out my book “Python One-Liners”! πŸ™‚ This document contains many interactive code shells and videos to help you with your understanding. However, it’s pretty slow because of all … Read more

The Most Pythonic Way to Check If a List Contains an Element

The standard way of checking if an element exists in a list is to use the in keyword. For example, ‘Alice’ in [1, ‘Alice’, 3] will return True while the same returns False for ‘Bob’. If you need to check more complicated conditions, use the any(condition(x) for x in list) function with a generator expression. … Read more

Does the Dot Regex (.) Match Whitespace Characters in Python?

Yes, the dot regex matches whitespace characters when using Python’s re module. Consider the following example: Try it yourself in our interactive Python shell (click “run”): The dot matches all characters in the string–including whitespaces. You can see that there are many whitespace characters ‘ ‘ among the matched characters. Need more info? Watch the … Read more

The Most Pythonic Way to Check if a Python String Contains Another String? (Tutorial + Video)

How to check if a Python string s1 contains another string s2? There are two easy ways to check whether string s1 contains string s2: You can try both methods in our interactive Python shell (just click “Run” to execute the code in your browser): Puzzle: Can you modify “Method 2” so that it also … Read more

The Dot Character in a Character Set – What Does It Match?

Given is the following regular expression: Note the dot character inside the character set. As you may know, the dot metacharacter matches an arbitrary character if it is used outside a character set. But what does it match if you place the dot character inside a regex character set? The answer is that the dot … Read more