Python Dictionary Append – 4 Best Ways to Add Key/Value Pairs

You can add multiple key-value pairs to a Python dictionary by using the update() method and passing a dictionary as an argument. For example, dict1.update(dict2) will insert all key-value pairs of dict2 into dict1. You can add a single key:value pair to a Python dictionary by using the square bracket approach dict[key] = value. In … Read more

Proper Indentation for Python Multiline Strings

Python Mutliline Strings In this article we are going to look at how to properly indent your code for Python multiline strings, so let’s start by clarifying what a multiline string is and why indentation is important. Readability for other users is one of the key requirements of writing effective Python code, so having a … Read more

How to Print a String Without ‘\n’ in Python

Strings, Printing and ‘\n’ The printing of a basic string is probably the first program the majority of people, myself included, write in Python –  does print(‘hello world’) sound familiar? From the outset we know the principle is simple, we can pass our string through the print() function and the output will be displayed in … Read more

Check if All Characters of a String are Uppercase

Problem Formulation: How to check if all characters of a string are uppercase? Background: A string is a sequence of characters, and is amongst the most commonly used and popular data types in Python. Strings can be enclosed by either single or double quotes and are β€˜immutable’, meaning they can’t be changed once created. There … Read more

Python Get Milliseconds

In this article we are going to look at how we can see time in milliseconds,Β  i.e. one thousandth (1000th) of a second, using Python. This could be for pure curiosity, benchmarking code or to getΒ  a very, very accurate time of day –Β  whatever the reason, the focus of the article is how to … Read more

How to Import Libraries in Python’s exec() Function?

What is the exec() Function exec() is a built-in Python function that is most commonly used to dynamically execute code,  either as a string or object code. To properly understand how we can use exec() to import libraries and modules we need to familiarize ourselves with the syntax of the function itself, as it becomes relevant later: … Read more