7 Best Ways to Use Newline in an F-String

Problem Formulation In the following example, you use a newline character ‘\n’ inside the curly braces of an f-string: But if you run this code, Python raises a SyntaxError: f-string expression part cannot include a backslash. ⚑ The reason is that there is a limitation in versions of Python before 3.12 where backslashes are not … Read more

Regex to Extract All Email Addresses from a String βœ… (Tutorial + Online Tool)

I have just created this small helper tool to extract all email addresses from a huge text file or string. Simply copy and paste your text into the field and click “Extract Emails“: πŸ‘‡ Email Extractor Enter Text: Extract Emails Copy to Clipboard Extracted Emails: JavaScript Regex In the online tool above, I used a … Read more

Python String Comprehension: A Comprehensive Guide

Python String Comprehension is a concise way to create strings using a single line of code, often involving iteration and optional conditionals, providing a succinct, readable alternative to using loops. It’s particularly useful for generating strings based on existing iterables. Here’s an example: In this example, the generator expression (fruit[0] for fruit in input_list if … Read more

Best Ways to Remove Unicode from List in Python

When working with lists that contain Unicode strings, you may encounter characters that make it difficult to process or manipulate the data or handle internationalized content or content with emojis 😻. In this article, we will explore the best ways to remove Unicode characters from a list using Python. You’ll learn several strategies for handling … Read more

4 Best Ways to Remove Unicode Characters from JSON

To remove all Unicode characters from a JSON string in Python, load the JSON data into a dictionary using json.loads(). Traverse the dictionary and use the re.sub() method from the re module to substitute any Unicode character (matched by the regular expression pattern r'[^\x00-\x7F]+’) with an empty string. Convert the updated dictionary back to a … Read more

Python Repeat String Until Length

The multiplication operator (*) allows you to repeat a given string a certain number of times. However, if you want to repeat a string to a specific length, you might need to employ a different approach, such as string slicing or a while loop. For example, while manipulating strings in Python, you may need to … Read more

Write a Long String on Multiple Lines in Python

To create and manage multiline strings in Python, you can use triple quotes and backslashes, while more advanced options involve string literals, parentheses, the + operator, f-strings, the textwrap module, and join() method to handle long strings within collections like dictionaries and lists. Let’s get started with the simple techniques first: πŸ‘‡ Basic Multiline Strings … Read more

How to Remove the Percentage % Sign and Convert to Number in Python

Problem Formulation Picture this: you’re extracting data from an XML file with a series of percentages. But your percentages look something like this: You’ve successfully extracted the data, but these percentages are tagged with the “%” symbol, making them strings rather than numbers. Now, let’s say you want to do something like: But with that … Read more