Python: How to Convert a Float to Binary (2 Ways)
Updated: Jun 03, 2023
This concise article shows you 2 different ways to convert a float to binary in Python (both of these methods only use built-in features of Python and don’t require any third-party libraries). Using the struct module This......
Python: 4 Ways to Round a Number
Updated: Jun 03, 2023
Using the round() function round() is a built-in function of Python that provides a simple and straightforward way to round a number to a given precision in decimal digits. Example: number = 1.23456 # Round to 3 decimal......
Python: Generate a Random Integer between Min and Max
Updated: Jun 03, 2023
To generate a random integer between two given integers, the quickest and most convenient way is to use the randint() function from the random module. The minimum and maximum values are both included (that means one of them has a chance......
Python: Generate a Random Float between Min and Max (3 ways)
Updated: Jun 03, 2023
This practical article shows you 3 different ways to generate a random floating point number in a given range between a min value and a max value. All of these approaches are quick and simple. Let’s get started. Using......
Python: Replace unwanted words in a string with asterisks
Updated: Jun 03, 2023
When developing web and other kinds of applications with Python, there might be cases where you want (or have to) substitute bad words with asterisks (*), such as: Censorship: In cases where you need to filter sensitive or......
Python: Get a list of unique words/characters from a string
Updated: Jun 03, 2023
When working with language-related tasks in Python, you may need to get a list of unique words or characters from a string to perform word frequency analytics, tokenization, deduplication, vocabulary creation, or data cleaning and......
Python: How to Reverse the Order of Words in a String
Updated: Jun 03, 2023
This concise, step-by-step guide shows you how to reverse the order of words in a string in Python. The steps are: Split the string into individual words using the split() method. By default, split() splits the string on......
Python: 3 Ways to Add Leading Zeros to a String
Updated: Jun 02, 2023
In Python programming, adding leading zeros (zeros left-padding) to a string is useful in various scenarios, such as when maintaining consistent formatting, sorting values, or ensuring proper alignment in numerical data or timestamps. It......
Python: How to Algin a String (Left, Right, and Center)
Updated: Jun 02, 2023
This pithy tutorial will show you a couple of different ways to align left, align right, or center a string in Python. Text width & length Before getting started, let me clarify some things. In Python, the width of a string is......
Python: Get Hostname and Protocol from a URL
Updated: Jun 02, 2023
This concise article shows you how to get the hostname and the protocol from a given URL in Python. Before writing code, let me clarify some things. Suppose we have a URL like......
Python: 3 Ways to Get File Name and Extension from URL
Updated: Jun 02, 2023
Suppose you have a file URL like this: https://api.slingacademy.com/v1/sample-data/files/marketing-campaigns.csv Or like this: https://api.slingacademy.com/public/sample-photos/1.jpeg How can you......
Python: 3 ways to extract all URLs from text
Updated: Jun 02, 2023
This succinct practical article will show you a couple of different ways to extract all URLs from a given string in Python. These techniques might be helpful in many scenarios, such as web scraping or text preprocessing in natural......