Python | Split String by Newline

Summary: Use given_string.splitlines() to split a given string by newline. Minimal Example: Problem Formulation πŸ“œProblem: Given a string, How will you split the string into a list of words using newline as a separator/delimiter? Example: Let’s dive into the different ways of solving the given problem. Method 1: Using splitlines Approach: The easiest way to split a … Read more

Python | Split String by Comma and Whitespace

Summary: To split a string by comma and whitespace, you can use a list comprehension that splits the given string by comma and then eliminate the whitespaces from the substrings using the strip method. Minimal Example: Note that there are different scenarios and many other ways of solving the problem. Please read ahead to discover … Read more

Python Create List of Objects

You can create a list of objects using the list comprehension statement [MyObject() for _ in range(n)] that repeats n times the creation of a new object using the MyObject() constructor. The result is a list of n new objects (not copies). Here’s a minimal example: You can see that all of them point to … Read more

How to Get the First Character of a String

Problem Formulation and Solution Overview This article will show different ways to extract the first character of a Python string. To make it more interesting, we have the following scenario: The Government has asked Rivers Clothing to complete forms for each employee. These forms require the first character of the employee’s first name and their … Read more

Python bytes vs bytearray

What’s the Difference Between bytes() and bytearray()? The difference between bytes() and bytearray() is that bytes() returns an immutable and bytearray() returns a mutable object. So you can modify a bytearray but not bytes type. Here’s a minimal example that nicely demonstrates the difference of the two functions: You create two variables a and b. … Read more

Working with Markdown Files in Python

This article will show you how to create and work with markdown files using Python. πŸ’‘ Markdown is an excellent tool with many features to spice up a flat-text file, such as changing text colors, adding bullet points, tables, and much more. A terrific way to add pizzazz to an otherwise dull file. To make … 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

How to Add Elements to a Python Set

Problem Formulation and Solution Overview This article will show you how to add elements to a Python set. ℹ️ Info: A Python set is a collection of unique item(s) saved in no particular order (unordered). A set cannot be changed. However, elements can be added and removed. Sets can also perform calculations like union, intersection, … Read more