How To Apply A Function To Each Element Of A Tuple?

This article shows you how to apply a given function to each element of a tuple. The best way to apply a function to each element of a tuple is the Python built-in map(function, iterable) function that takes a function and an iterable as arguments and applies the function to each iterable element. An alternate … Read more

Replace Last Substring Occurrence in Python String

Problem Formulation Given a string, a substring, and a replacement string in Python. String s Substring sub Replacement string repl How to find and replace the last occurrence of sub with the replacement repl in the Python string s? Let’s have a look at a couple of examples to thoroughly understand the problem: Example 1: … Read more

Find Index of Last Substring Occurrence in Python String

Problem Formulation Given a string and a substring in Python. How to find the index of the last occurrence of the substring in Python? Let’s have a look at a couple of examples to thoroughly understand the problem: Example 1: string = ‘fifi’ substring = ‘fi’ result: 2 Example 2: string = ‘hello’ substring = … Read more

How to List All Files ONLY in the Current Directory?

Problem Formulation How to list all files in the current directory given these constraints: Non-Recursive: You do not want to list files in subdirectories. ONLY Files: You do not want to list folder names. Current Directory: You run the Python script from the current directory. Here’s an example structure: current_folder — code.py — file.txt — … Read more

How to Make a Beep Sound in Python? [Linux/macOS/Win]

Windows: How to Make Beep in Python To make a beep sound in Python on your Windows machine: Import the library using: import winsound Call windsound.Beep(frequency, duration) for your desired frequency (in Hertz) and duration (in milliseconds). For example, winsound.Beep(2000, 1500) would make a sound with 2000 Hz for 1.5 seconds. Here’s the relevant code … Read more

How to Remove a Trailing Newline?

Use str.rstrip() to remove a trailing newline. Given a string s, create a new one without trailing newline character by calling s.rstrip(‘\n’). If you need the string stored in the original variable, simply assign the result to the original variable. Here’s an example: This actually removes all trailing newline characters: If you want to remove … Read more

Python __aiter__() and __anext__() Magic Methods

object.__aiter__(self) object.__anext__(self) πŸ’‘ Summary: Python’s __aiter__() and __anext__() methods are used to implement an asynchronous for loop (keywords: async for). In contrast to a normal (synchronous) for loop, an asynchronous for loop iterates over an asynchronous source. __aiter__() returns an asynchronous iterator object (in many cases it’s simply a reference to itself: return self) __anext__() … Read more

Python __aexit__() Magic Method

object.__aexit__(self, exc_type, exc_val, exc_tb) πŸ’‘ Summary: Python’s __aexit__() magic method is semantically similar to __exit__() but is used for asynchronous and parallel programming. Python calls the __aexit__() magic method when leaving an async with block whereas the __aenter__() method is called when entering it. An object that implements both __aenter__() and __aexit__() methods is called … Read more

Python __aenter__() Magic Method

object.__aenter__(self) πŸ’‘ Summary: Python’s __aenter__() magic method is semantically identical to __enter__() but is used for asynchronous and parallel programming. Python calls the __aenter__() magic method when starting an async with block whereas the __aexit__() method is called when leaving it. An object that implements both __aenter__() and __aexit__() methods is called an asynchronous context … Read more

Python __exit__() Magic Method

object.__exit__(self, exc_type, exc_value, traceback) πŸ’‘ Summary: Python calls the __exit__() magic method when ending a with block whereas the __enter__() method is called at the start. An object that implements both __exit__() and __enter__() is called a context manager. By defining those methods, you can create your own context manager. We define a custom class … Read more