How to Convert a Dictionary to a String in Python

Convert a dict to a string in Python: Command Prompt output comparing str() and json.dumps() for the same dictionary

To convert a dict to a string in Python, use json.dumps(d) when the text will be stored, sent to an API or read by another program, and str(d) for a quick, Python-style text you only want to print or log. For a custom format such as key=value pairs, join the items yourself. This guide compares … Read more >>

Find an Armstrong Number Using Recursion in Python

armstrong number

Recently, while teaching Python to a group of data enthusiasts in New York, one of my students asked me how to find an Armstrong number using recursion in Python. At that moment, I realized that while many people know how to check an Armstrong number using loops, very few understand how to do it recursively, … Read more >>

How to Swap Two Numbers in Python (With and Without a Function)

Swap two numbers in Python: Command Prompt output of tuple unpacking, a temp variable, arithmetic and XOR swaps

To swap two numbers in Python, use tuple unpacking: a, b = b, a. It is one line, works for any type (integers, floats, strings, list items) and needs no temporary variable. This guide shows that and the classic alternatives (a temporary variable, arithmetic and XOR), explains how swapping works inside a function, and includes … Read more >>

Insert Multiple Elements into a List in Python

python append multiple items to list

I was working on a Python project where I had to add several items to a shopping cart list at once. The challenge was simple, but I realized there are multiple ways to insert multiple elements into a Python list. Since I have been using Python for more than 10 years, I’ll walk you through … Read more >>

Create a String With NewLine in Python

reverse list python

When I first started coding in Python more than a decade ago, one of the things that often confused me was how to properly add a new line inside a string. Sometimes I wanted to display text neatly on multiple lines. Other times, I needed to format data for reports where each entry had to … Read more >>

How to Reverse a List in Python

reverse list python

Over the years of working with Python, I’ve often come across situations where I needed to reverse the order of items in a list. At first, it might sound like a simple task. But depending on the project, the way you reverse a list in Python can make a big difference in performance and readability. … Read more >>

Extract Strings from a List in Python

extract string from list python

As a developer, I was working on a project where I had to clean up a dataset that mixed numbers, strings, and special characters in the same list. So, I had to rely on different Python tricks and methods I’ve learned over the years. In this tutorial, I’ll share those methods with you. I’ll show … Read more >>

Get All Values from a Dictionary in Python

python dictionary get values

When I first started working with Python dictionaries more than a decade ago, I often found myself needing to extract just the values. Sometimes I wanted to analyze customer data stored in a dictionary. Other times, I needed to quickly convert values into a list for further processing. Over the years, I’ve discovered multiple ways … Read more >>

How to Get File Extension in Python?

python get file type

When I was working on a project, I had to process hundreds of files stored in a shared folder. The challenge was simple: I needed to identify file extensions before performing any operation. At first, I thought Python would have an easy function for it. However, I soon realized that there are multiple ways to … Read more >>