How to Repeat a String Multiple Times in Python

Problem Formulation and Solution Overview In this article, you’ll learn how to repeat a string multiple times in Python. Over your career as a Python coder, you will encounter situations when a string needs to be output/displayed a specified number of times. The examples below offer you various ways to accomplish this task. 💬 Question: … Read more

Python’s && Equivalent: Logical And

In Python, the equivalent of && (logical-and) in programming languages such as C++ or Java in an if-statement is and. Using && will raise a SyntaxError, so use and instead! Overview Problem: What is the equivalent of &&(logical-and) in Python? Example: Let’s have a look at the following example: Output: You will get the following … Read more

Python __init_subclass__() Magic Method

The Python class.__init_subclass__(cls) method is called on a given class each time a subclass cls for that class is created. Syntax class.__init_subclass__(cls) We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this … Read more

Python __subclasscheck__() Magic Method

Syntax class.__subclasscheck__(self, subclass) Python’s class.__subclasscheck__(self, subclass) method implements the issubclass(subclass, class) built-in function. It should return True if subclass is a direct or indirect subclass of class using Python inheritance to define the subclass relationship. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all … Read more

Python __sizeof__() Method

Syntax object.__sizeof__(self, other) The Python __sizeof__() method returns the size of the object in bytes. The sys.getsizeof() method internally call’s __sizeof__() and adds some additional byte overhead, e.g., for garbage collection. Basic Example The following code snippet creates a list object x with three integers and measures its size in bytes (104) using the x.__sizeof__() … Read more

Python __trunc__() Magic Method

Syntax and Definition object.__trunc__(self) The Python __trunc__() method implements the behavior of the math.trunc() function. For example, if you attempt to call math.trunc(x), Python will run the x.__trunc__() method to obtain the return value. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more

Python __floor__() Magic Method

Syntax and Definition object.__floor__(self) The Python __floor__() method implements the behavior of the math.floor() function. For example, if you attempt to call math.floor(x), Python will run the x.__floor__() method to obtain the return value. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more

Python __ceil__() Magic Method

Syntax and Description object.__ceil__(self) The Python __ceil__() method implements the behavior of the math.ceil() function. For example, if you attempt to call math.ceil(x), Python will run the x.__ceil__() method to obtain the return value. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder … Read more

Python __round__() Magic Method

Syntax object.__round__(self, ndigits=0) The Python __round__() method implements the built-in round() function. For example, if you attempt to call round(x) or round(x, ndigits), Python will run the x.__round__() or x.__round__(ndigits) method, respectively. The following code snippet overrides the __round__() dunder method to return the rounded age of a Person when you pass an object of … Read more