Python | Split String Carriage Return

⭐Summary: The most efficient way to split a string at carriage return (“\r”) is to use the split function like so given_string.split(). An alternate approach is to use different functions of the regex package to split the string at multiple whitespaces. Minimal Example Problem Formulation 📜Problem: Given a string. How will you split the string at carriage return? … Read more

Python | Split String Multiple Whitespaces

🍎Summary: The most efficient way to split a string using multiple whitespaces is to use the split function like so given_string.split(). An alternate approach is to use different functions of the regex package to split the string at multiple whitespaces. Minimal Example: Problem Formulation 📜Problem: Given a string. How will you split the string using … Read more

Python | Split String then Join

Summary: Use one of the following ways to split a string to get an element from a string: Minimal Example Problem Formulation Example Challenge: Extract certain substrings from the given string and then combine the extracted substrings with a “/” in between to create a new string. Thus, you first have to split the string … Read more

Python | Split String and Keep Whitespace

⭐Summary: To split a string and keep the delimiters/separators, you can use one of the following methods: (i) Using the regex package and its functions. (ii) Using a list comprehension. Minimal Example Problem Formulation Problem: Given a string in Python. How to split the string and also keep the spaces? Example Consider that there’s a … Read more

Python | Split String after Character

Problem Formulation 📜 Problem: Given a string. How will you split the string after a specific character? Note that there can be different scenarios that can come up when you are trying to split a string after a certain character/sub-string. So, let’s understand the given problem with the help of numerous examples. 🏷️Scenario 1: Splitting … Read more

Python Regex Named Groups

Before we dive into named groups, let’s quickly recap normal capture groups in Python. If you already know normal “unnamed capturing groups” well, feel free to skip the first section and move right away to the next one about named capture groups in Python. 👇 What Are Python Regex Groups? A normal Python regex group … Read more

How to Remove Text Within Parentheses in a Python String?

Problem Formulation and Solution Overview This article will show you how to remove text within parentheses in Python. To make it more interesting, we have the following running scenario: Rivers Clothing has a CSV file containing all their employees. The format is currently first name (middle name) and last name (for example, Martin (Robert) Simpson). … Read more

How to Clean and Format Phone Numbers in Python

Problem Formulation and Solution Overview This article will show you how to create, clean up, and format phone numbers in Python. To make it more interesting, we have the following scenario. Right-On Realtors has a Contact Us form on their main website containing several fields. One of these fields is a Phone Number. Upon submission, … Read more

Python – How to Find the Longest Substring in Alphabetical Order?

Programming Challenge 💬 Challenge: Given a Python string. Find the longest substring for which holds that all of its characters are in alphabetical order so that the first character comes earlier in the alphabet than the second character, and so on. Here are three examples: You get the point. 🙂 Next, I’ll show you three … Read more

How to Find a Partial String in a Python List?

Problem Formulation 💬 Challenge: Given a Python list of strings and a query string. Find the strings that partially match the query string. Example 1: Input: [‘hello’, ‘world’, ‘python’] and ‘pyth’ Output: [‘python’] Example 2: Input: [‘aaa’, ‘aa’, ‘a’] and ‘a’ Output: [‘aaa’, ‘aa’, ‘a’] Example 3: Input: [‘aaa’, ‘aa’, ‘a’] and ‘b’ Output: [] … Read more