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: string: ‘helloworld’, longest substring: ‘ellow’ string: ‘abcde’, longest substring: ‘abcde’ … 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

How to Convert Tab-Delimited File to CSV in Python?

The easiest way to convert a tab-delimited values (TSV) file to a comma-separated values (CSV) file is to use the following three lines of code: import pandas as pd df = pd.read_csv(‘my_file.txt’, sep=’\t’, header=None) df.to_csv(‘my_file.csv’, header=None) We’ll explain this and other approaches in more detail next—scroll down to Method 3 for this exact method. Problem … Read more

Python RegEx – Match Whitespace But Not Newline

Problem Formulation šŸ’¬ Challenge: How to design a regular expression pattern that matches whitespace characters such as the empty space ‘ ‘ and the tabular character ‘\t’, but not the newline character ‘\n’? An example of this would be to replace all whitespaces (except newlines) between a space-delimited file with commas to obtain a CSV. … Read more

How to Convert Space-Delimited File to CSV in Python?

The easiest way to convert a space-delimited file to a comma-separated values (CSV) file is to use the following three lines of code: import pandas as pd df = pd.read_csv(‘my_file.txt’, sep=’\s+’, header=None) df.to_csv(‘my_file.csv’, header=None) We’ll explain this and other approaches in more detail next—scroll down to Method 3 for this exact method. Problem Formulation Given … Read more

How to Find All Matches using Regex

Problem Formulation and Solution Overview In this article, you’ll learn how to find all matches in a string using regex. The Regular Expression, also referred to as regex, is a complex pattern to search for and locate matching character(s) within a string. At first, this concept may seem daunting, but with practice, regex will improve … Read more

How to Replace Whitespaces with Underscores

Problem Formulation and Solution Overview In this article, you’ll learn how to replace whitespaces with underscores in Python. To make it more fun, we have the following running scenario: Bryan, an IT Instructor, has given his students a Python coding challenge: Take a famous quote and replace all whitespaces with underscores in four (4) ways: … Read more

You Cannot Use Python Regex in startswith(). Do This Instead.

Iā€™m sitting in front of my computer refactoring Python code and have just thought of the following question: Can You Use a Regular Expression with the Python string.startswith() Method? The short answer is no. The string.startswith() method doesn’t allow regular expression inputs. And you don’t need it because regular expressions can already check if a … Read more