How to Convert a Unicode String to a Dictionary in Python?

Problem Formulation Given a Unicode string representation of a dictionary. How to convert it to a dictionary? Input: u”{‘a’: 1, ‘b’: 2, ‘c’: 3}” Output: {‘a’: 1, ‘b’: 2, ‘c’: 3} Note: The u’string’ representation represents a Unicode string that was introduced in Python 3. This is redundant as all strings in Python 3 are … Read more

Python Print Octal Without ‘0o’

Problem Formulation If you print an octal number, Python uses the prefix ‘0o’ to indicate that it’s a number in the octal system and not in the decimal system like normal integers. However, if you already know that the output numbers are octal, you don’t necessarily need the ‘0o’ prefix. How to print oct numbers … Read more

Python Print Hex Without ‘0x’

Problem Formulation If you print a hexadecimal number, Python uses the prefix ‘0x’ to indicate that it’s a number in the hexadecimal system and not in the decimal system like normal integers. However, if you already know that the output numbers are hexadecimal, you don’t necessarily need the ‘0x’ prefix. How to print hex numbers … Read more

Python Print Binary Without ‘0b’

Problem Formulation If you print a binary number, Python uses the prefix ‘0b’ to indicate that it’s a number in the binary system and not in the decimal system like normal integers. However, if you already know that the output numbers are binary, you don’t necessarily need the ‘0b’ prefix. How to print binary numbers … Read more

Python Print Without Quotes

Problem Formulation In Python’s interactive mode, each line is assumed to be an expression that is evaluated. The return value is provided to the user. Thus, if you evaluate a string expression or call a function or an operation that returns a string, the output will display quotes around the string to tell the user … Read more

How to Print a Float Without Scientific Notation in Python?

Problem Formulation If you print a float value in Python that is smaller than 0.0001, Python will use the scientific notation for small numbers such as 1e-05 that is short for 1*1/10**-5. Here’s an example output when printing smaller and smaller floats to the shell. If there are more than three zeros after the decimal … Read more

How to Serialize a Python Dict into a String and Back?

Problem Formulation Given a Python dictionary containing lists and other data structures. You want to store the dictionary in a file or send it over the network in a more efficient form. How to serialize a Python dictionary into a string, and then deserialize the string back to a dictionary data structure? Here’s a rough … Read more

Reverse a String in Python

There are four main ways to reverse a string in Python: Slicing s[::-1] with negative step size traverses the string from right to left. ”.join(reversed(s)) first creates an iterable of characters in reversed order, and then joins these characters to obtain the reversed string. A for loop using the range(len(s)-1, -1, -1) function traverses the … Read more

What Are Differences Between type() and isinstance()?

The main difference between type() and isinstance() is that type(object) returns the type of an object and isinstance(object, class) returns True if the object argument is an instance of the class argument or in a direct or indirect subclass relationship. To strengthen your understanding, let’s quickly recap the syntactical definitions of both functions: type(object) – … Read more