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

Suppose you are developing a user interface, and you support it with your code. Your user is uploading a document, and your code needs to wait for the time the file is being uploaded. Also, when you visit a complex having automated doors, you must have noticed that while you are entering the complex, the […]

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 [javascript] var null_variable = null; [/javascript] null in PHP [php] $null_variable […]

Read More

How to 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()

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