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 … Read more

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 … Read more

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

Python Print Without Parentheses

Parentheses are used to initiate a function call in Python. Here are three examples: f() calls custom function f without an argument, print(‘hello world’) calls built-in function print with the string argument ‘hello world’, and range(2, 10, 3) calls built-in function range on the integer arguments 2, 10, and 3. A common question among Python … Read more

Python Print Without Space

Problem Formulation Python’s print() function allows an arbitrary number of comma-separated values and prints them to the shell, separated by a single empty space character ‘ ‘. The following example shows how you pass four string values as arguments into the print() function: The resulting shell output has an added empty space character ‘ ‘ … Read more