Python’s string.replace() Method – Replacing Python Strings

Replacing Python Strings Often you’ll have a string (str object), where you will want to modify the contents by replacing one piece of text with another. In Python, everything is an object – including strings. This includes the str object. Luckily, Python’s string module comes with a replace() method. The replace() method is part of […]

Read More

Validate Python Function Parameter & Return Types with Decorators

Overview So I was playing around with Python decorators the other day (as you do). I always wondered if you could get Python to validate the function parameter types and/or the return type, much like static languages. Some people would say this is useful, whereas others would say it’s never necessary due to Python’s dynamic […]

Read More

Python’s range() Function Explained

What is Python’s range() Function? As an experienced Python developer, or even a beginner, you’ve likely heard of the Python range() function. But what does it do? In a nutshell, it generates a list of numbers, which is generally used to iterate over with for loops. There’s many use cases. Often you will want to […]

Read More

Python’s time.sleep() – Pause, Stop, Wait or Sleep your Python Code

Python’s time module has a handy function called sleep(). Essentially, as the name implies, it pauses your Python program. time.sleep() is the equivalent to the Bash shell’s sleep command. Almost all programming languages have this feature, and is used in many use-cases. Python’s time.sleep() Syntax This is the syntax of the time.sleep() function:

time.sleep() […]

Read More

Python’s null equivalent: None

What is the null or None Keyword The null keyword is commonly used in many programming languages, such as Java, C++, C# and Javascript. It is a value that is assigned to a variable. Perhaps you have seen something like this: null in Javascript

null in PHP

null in Java

The concept […]

Read More

Check if a file exists in a directory with Python

With Python there are several methods which can be used to check if a file exists, in a certain directory. When checking if a file exists, often it is performed right before accessing (reading and/or writing) a file. Below we will go through each method of checking if a file exists (and whether it is […]

Read More

Check if a String is a Number in Python with str.isdigit() (and Unicode)

Edit 1: This article has been revised to show the differences between str.isdigit() and the custom solution provided. Edit 2: The sample also supports Unicode! Often you will want to check if a string in Python is a number. This happens all the time, for example with user input, fetching data from a database (which […]

Read More