Python String find() Method: Syntax and 4 Examples

Python string find() method returning the index of a substring and -1 when it is missing

If you want the position of a substring in Python, the Python find method is what you need. It takes the substring and gives you back the index of the first match, or -1 when it isn’t there. In this tutorial, I’ll cover the find() syntax plus four examples: a basic search, start and end … Read more >>

How to Print Bytes as Hex in Python

Python print bytes as hex: Command Prompt output of bytes.hex with separators and uppercase

To print bytes as hex in Python, call .hex() on the bytes object. Pass a separator, like data.hex(” “), if you want a space between each byte: I ran every snippet on Python 3.12.5; the separator arguments need Python 3.8 or newer. You’ll see bytes.hex() with separators, why print() shows odd \x escapes, per-byte formatting … Read more >>

Python range() Function: Syntax and 5 Examples

Python range() function turning range(5) and range(1, 11) into lists of integers

If you want a sequence of whole numbers in Python, the Python range function is what you need. It takes a stop number and gives you back 0, 1, 2 and so on, stopping just before that number. In this tutorial, I’ll cover its syntax and five examples: start and stop, steps, counting down, looping … Read more >>

Mann-Whitney U Test in Python (SciPy Examples)

Mann-Whitney U test in Python: Command Prompt output of scipy.stats.mannwhitneyu with U and p-value

To run a Mann-Whitney U test in Python, pass your two independent samples to scipy.stats.mannwhitneyu(). It returns the U statistic and a p-value telling you whether one group tends to have larger values: My examples use Python 3.12.5 and SciPy 1.18.1, with order values from a Chicago store that tested a new checkout layout. We’ll … Read more >>

Password Validation in Python: Regex, NIST Rules and a Checker

Password validation in Python: VS Code output of a NIST-style length and blocklist check on demo passwords

If your sign-up form rejects long passphrases but accepts Password1!, its password validation in Python needs a fix. The usual fix, one regex demanding mixed character types, checks the wrong things. It gives no reason and rewards short, predictable passwords. Quick answer: write a function that returns what’s wrong, and put length and a common-password … Read more >>

Python Email Validation: How to Validate Email Addresses in Python

Python email validation: Command Prompt output of email-validator accepting and rejecting newsletter sign-ups

If you’ve tried the usual one-line regex for Python email validation, you’ve probably seen it accept ava..lopez@example.com and even liam carter@example.com, space and all. The fix is the email-validator package: validate_email() returns a cleaned-up address or raises EmailNotValidError with a plain-English reason. It can also check that the domain accepts mail. In this tutorial, I’ll … Read more >>

Python classmethod vs staticmethod: Class Method vs Static Method

Python classmethod vs staticmethod: VS Code output of an Order class changing its tax rate and checking a ZIP code

If a Python method needs the class but not one object, you have to pick between two decorators: classmethod vs staticmethod. Use @classmethod when the method needs the class itself: shared settings, alternate constructors, subclasses. Use @staticmethod when it needs neither the class nor an object, like the ZIP check. In this tutorial, I’ll compare … Read more >>

React Children: How to Use a React Component with Children

React component with children: two Panel wrappers rendering order and stock content passed as children

A React component with children is a wrapper: whatever you put between its opening and closing tags arrives in the children prop. Render {children} where that content should appear: In this tutorial, I’ll use a small store dashboard to show how React children work, how to type them in TypeScript, and how React.Children counts and … Read more >>

TypeScript Readonly Array: readonly T[] and ReadonlyArray

TypeScript readonly array: VS Code showing compile errors for push, index writes and sort

A checkout bug I fixed in a Seattle store’s TypeScript code came down to one helper that called sort() on the shared list of shipping states. Declaring the list with const didn’t help, since const locks the variable, not the items. A TypeScript readonly array stops that at compile time, because its type has no … Read more >>

Bisection Method Code in Python (With Examples)

Bisection method code in Python: Command Prompt table of iterations converging on a root

The bisection method code in Python finds a root of f(x) by halving an interval [a, b] where the function changes sign, keeping the half that still contains the root, until the interval is tiny: I ran each example with Python 3.12.5 and SciPy 1.18.1, and I checked my own function against SciPy’s built-in solver. … Read more >>