Python Int to String with Leading Zeros

To convert an integer i to a string with leading zeros so that it consists of 5 characters, use the format string f'{i:05d}’. The d flag in this expression defines that the result is a decimal value. The str(i).zfill(5) accomplishes the same string conversion of an integer with leading zeros. Challenge: Given an integer number. … Read more

How To Extract Numbers From A String In Python?

The easiest way to extract numbers from a Python string s is to use the expression re.findall(‘\d+’, s). For example, re.findall(‘\d+’, ‘hi 100 alice 18 old 42’) yields the list of strings [‘100′, ’18’, ’42’] that you can then convert to numbers using int() or float(). There are some tricks and alternatives, so keep reading … Read more

Python Matplotlib Makes Conway’s Game of Life Come Alive

In this article, you’ll learn how to make this beautiful and interesting animation using only Python, NumPy, and Matplotlib — nothing else: πŸ‘‡ But how does the Game of Life work – and what’s behind the classical visualization anyways? The Game of Life Conway’s Game of Life is a cellular automaton devised by the British … Read more

Python – How to Find the Longest Substring in Alphabetical Order?

Programming Challenge πŸ’¬ Challenge: Given a Python string. Find the longest substring for which holds that all of its characters are in alphabetical order so that the first character comes earlier in the alphabet than the second character, and so on. Here are three examples: You get the point. πŸ™‚ Next, I’ll show you three … Read more