# Python String

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

Problem Formulation Given a string representation of a dictionary. How to convert it to a dictionary? Input: ‘{“1”: “hi”, “2”: “alice”, “3”: “python”}’ Output: {‘a’: 1, ‘b’: 2, ‘c’: 3} Method 1: eval() The built-in eval() function takes a string argument, parses it as if it was a code expression, and evaluates the expression. If …

## 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 …

## Python Print Octal Without ‘0o’

Problem Formulation If you print a hexadecimal 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 …

## 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 …

## 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 … ## How to Interleave Two Strings of Variable Lengths in Python?

Half an hour ago, my friend and coauthor of the textbook “Coffee Break NumPy” asked me the following question via WhatsApp: Problem Formulation How would you solve the problem of interleaving two strings in Python: Input: String s1= “AAA” and string s2 = “BBBBB” Output: String s=”ABABABBB” Being obsessed with finding the most Pythonic way …

## How to Print a NumPy Array Without Brackets in Python?

Note that this tutorial concerns NumPy arrays. To learn how to print lists without brackets check out this tutorial: How to Print a List Without Brackets in Python? Problem Formulation Given a NumPy array of elements. If you print the array to the shell using print(np.array([1, 2, 3])), the output is enclosed in square brackets …

## How to Print a Set Without Brackets in Python?

Problem Formulation Given a set of elements. If you print the list to the shell using print({1, 2, 3}), the output is enclosed in curly brackets (braces) like so: {1, 2, 3}. But you want the set without brackets like so: 1, 2, 3. How to print the set without enclosing brackets? Method 1: Unpacking …

## How to Print a Tuple Without Parentheses in Python?

Problem Formulation Given a tuple of elements. If you print the tuple to the shell using print((1, 2, 3)), the output is enclosed in parentheses like so: (1, 2, 3). But you want the list without brackets like so: 1, 2, 3. How to print the tuple without enclosing parentheses? Method 1: Unpacking The asterisk …

## How to Print a Dictionary Without Brackets in Python?

Problem Formulation Given a dictionary of key value pairs in Python. If you print the dictionary to the shell using print({‘a’: 1, ‘b’: 2}), the output is enclosed in curly brackets (braces) like so: {‘a’: 1, ‘b’: 2}. But you want the dictionary without brackets like so: ‘a’: 1, ‘b’: 2. How to print the …

## How to Print a List Without Brackets in Python?

Problem Formulation Given a list of elements. If you print the list to the shell using print([1, 2, 3]), the output is enclosed in square brackets like so: [1, 2, 3]. But you want the list without brackets like so: 1, 2, 3. How to print the list without enclosing brackets? Method 1: Unpacking The …

## Python Print Without Extra Newline When Reading a File

Problem Formulation Given a file with name file.txt and the following content: hi finxters! 🙂 You created a Python script that reads the file line-by-line and prints each line to the shell like so: But the output unexpectedly adds an extra newline to each line so you obtain the following output on the Python shell: …