Python exec() — A Hacker’s Guide to A Dangerous Function

Python’s exec() function executes the Python code you pass as a string or executable object argument. This is called dynamic execution because, in contrast to normal static Python code, you can generate code and execute it at runtime. This way, you can run programmatically-created Python code. Have you ever wondered about the limits of a … Read more

String Slicing in Python

String slicing is a concept to carve out a substring from a given string. Use slicing notation s[start:stop:step] to access every step-th element starting from index start (included) and ending in index stop (excluded). All three arguments are optional, so you can skip them to use the default values (start=0, stop=len(string), step=1). For example, the … Read more

Why Slicing With Index Out Of Range Works In Python?

Python slicing means to access a subsequence of a sequence type using the notation [start:end]. A little-known feature of slicing is that it has robust end indices. Slicing is robust even if the end index is greater than the maximal sequence index. The slice just takes all elements up to the maximal element. If the … Read more

Python String Concatenation Without ‘+’

When I first saw this, I was sure it’s a bug. Well—it’s a feature! In today’s short article, you’ll learn about a small Python trick that I call “string concatenation without +”. Let’s start with some code! Plus vs. Adjacent String Literal Concatenation There are two ways to concatenate string literals in Python: Using the … Read more

String Concatenation – An Interactive Guide

Python has powerful built-in capabilities for string manipulation. That’s why web companies like Google love Python—it’s a perfect fit for the text-based web. This guide shows you how to use string concatenation operators using multiple forms of education: Text Puzzle Video Exercise Ready to learn string concatenation? Let’s get started! A Textual Introduction to String … Read more

Python Slicing Bootscamp

Slicing is one of the most popular Python features. Thus, understanding slicing is key to understand existing code bases. In this tutorial, I’m going to train your slicing skills. Ready? So, let’s go! ? Watch the Video Python Slicing Read About Slicing Slicing is a Python-specific concept for accessing a range of values in sequence … Read more

Python Regex Finditer()

You can create an iterable of all pattern matches in a text by using the re.finditer(pattern, text) method: Specification: re.finditer(pattern, text, flags=0) Definition: returns an iterator that goes over all non-overlapping matches of the pattern in the text. The flags argument allows you to customize some advanced properties of the regex engine such as whether … Read more

Python Regex – How to Count the Number of Matches?

To count a regex pattern multiple times in a given string, use the method len(re.findall(pattern, string)) that returns the number of matching substrings or len([*re.finditer(pattern, text)]) that unpacks all matching substrings into a list and returns the length of it as well. A few hours ago, I wrote a regular expression in Python that matched … Read more

How to Generate Text Automatically With Python? A Guide to the DeepAI API

Do you want to enrich your Python script with powerful text-generation capabilities? You’re in the right place! What does it do? I just discovered DeepAI’s API that automatically generates a body of text, given a sentence fragment or topic keyword. How can it be used? You can use this as a basis to generate text … Read more

__str__ vs __repr__ In Python

Summary: The key goal of __str__ and __repr__ is to return a string representation of a Python object. The way they represent the string object differentiates them. str() & __str()__ return a printable/readable string representation of an object which is focused on the end-user. repr() & __repr()__ return a string representation of an object that is a valid Python object, something you can … Read more