Python Split String by Uppercase (Capital) Letters

✨Summary: You can use different functions of the regular expressions library to split a given string by uppercase letters. Another approach is to use a list comprehension.  Minimal Example Problem Formulation 📜Problem: Given a string containing uppercase and lowercase letters. How will you split the string on every occurrence of an uppercase letter? Example In … Read more

Python Split String Case Insensitive

✨Summary: Use the re.split function of the regex module to split the string using the given delimiter and set the regex flag re.IGNORECASE to ensure a case-insensitive split. Minimal Example Problem Formulation 📜Problem: Given a string containing a mixed blend of uppercase and lowercase characters. How will you split the string using a separator that … Read more

How to Interpolate Strings

Problem Formulation and Solution Overview This article will show you how to interpolate strings in Python. ℹ️ Interpolation is used when a string contains a placeholder for a variable or a calculation. The string is evaluated, and the placeholder is replaced by the passed data. To make it more interesting, we have the following running … Read more

Python Split String at Last Occurrence

⚡Summary:  Use given_string.rsplit(‘sep’, 1) to split the string at the last occurrence. Another approach to solve the problem is to use given_string.rpartition(‘sep’) Minimal Example: Problem Formulation  📜Problem: Given a string. How will you split the string at the last occurrence? Example Consider that you are given a string with multiple occurrences of the word “scream”. … Read more

Using PyTorch to Build a Working Neural Network

In this article, we will use PyTorch to build a working neural network. Specifically, this network will be trained to recognize handwritten numerical digits using the famous MNIST dataset. The code in this article borrows heavily from the PyTorch tutorial “Learn the Basics”. We do this for several reasons. Knowledge Background This article assumes the … Read more

Format Integer List to Hex String – Six Pythonic Ways

Problem Formulation 💬 Question: How to create a string of hex digits from a list of integers (0–255) so that each hex digit consists of two digits such as “00”, “01”, …, “fe”, “ff”? Here’s an example input/output pair: In: [0, 1, 2, 3, 255, 254, 253] Out: ‘00010203fffefd’ Method 1: Bytearray The easiest way … Read more

Python | Split String by Length

Summary: You can split a string by length by slicing the string into chunks of equal lengths. The indices to slice the string can be deduced using a list comprehension. Minimal Example: Problem Formulation 📜Problem: Given a string, how will you split the string by length? Here’s a related question asked in stack overflow that … Read more

7 Pythonic Ways to Copy a Dictionary

Problem Formulation and Solution Overview This article will show you how to copy a Dictionary in Python 7 different ways. To make it more interesting, we have the following running scenario: Mr. Smith, a High School Math Teacher, has developed an exciting way to grade multiple-choice exams. He has designed a Dictionary with the exam … Read more

Python decode()

This tutorial explains the Python decode() method with arguments and examples. Before we dive into the Python decode() method, let’s first build some background knowledge about encoding and decoding so you can better understand its purpose. 👇 Encoding and Decoding – What Does It Mean? Programs must handle various characters in several languages. Application developers … Read more