Python | List All Occurrences of Pattern in String

πŸ’¬ Question: Which method finds the list of all occurrences of the pattern in the given string? Problem Formulation Problem Formulation: Given a longer string and a shorter string. How to find all occurrences of the shorter string in the longer one? Consider the following example: Longer string: ‘Finxters learn Python with Finxter’ Shorter string … Read more

How to Assign the Result of exec() to a Python Variable?

πŸ’¬ Question: Say you have an expression you want to execute using the exec() function. How to store the result of the expression in a Python variable my_result? Before I show you the solution, let’s quickly recap the exec() function: Recap Python exec() Python’s exec() function executes the Python code you pass as a string … Read more

How to Print Colored Text in Python?

Summary: To print colored text in Python, you can use: The simple_color package, The ‘\033[1m’ ANSI escape-sequence, The termcolor module, The coloroma package, The colored library, The prompt_toolkit package. A simple, no-library way to print bolded text in Python is to enclose a given string s in the special escape sequence like so: print(“\033[38;5;4m”). We’ll … Read more

How to Extract Numbers from a String

Problem Formulation and Solution Overview In this article, you’ll learn how to extract numbers from a string in Python. To make it more fun, we have the following running scenario: This article references an Albanian proverb written by Driton Selmani in 2012. We will leave the interpretation up to you. πŸ’¬ Question: How would we … Read more