How do you format numbers with f-strings in Python?
Format numbers in f-strings with specs after a colon: decimals, thousands separators, percentages, zero padding, other bases, scientific notation and dynamic widths.
729 posts about Python: questions with answers, tutorials and articles, newest first.
Format numbers in f-strings with specs after a colon: decimals, thousands separators, percentages, zero padding, other bases, scientific notation and dynamic widths.
Fixtures are reusable setup functions that pytest injects into tests by name. Learn how to define them, combine them and use the built-in ones.
F-strings insert expressions into text with f and curly braces. See the syntax, the debug specifier, conversions, brace escaping, Python 3.12 rules and when to avoid them.
See which files, functions and classes pytest collects by default, how to change the rules, and how assert rewriting produces detailed failure messages.
Escape sequences are backslash codes like newline and tab inside string literals. See the full list, Unicode escapes, raw strings and how Python 3.12 treats invalid ones.
Install pytest, write a first test with a plain assert, check exceptions with pytest.raises and learn the command line options for running tests.
Python strings are immutable so they can be hashed, shared and optimised safely. Learn how to modify text with new strings, lists, join and bytearray.
Compare isdecimal(), isdigit() and isnumeric(), see why they reject signs and decimals, and validate numbers safely with int(), float(), Decimal and regular expressions.
Use str.count() to count non-overlapping substrings, then switch to startswith in a loop, a lookahead regex or Counter for overlapping, case-insensitive and word counts.
Check for a substring in Python with the in operator, then use startswith(), find() or re.search() when you need positions, endings or patterns. Case handling included.
Python changes string case with upper(), lower(), title(), capitalize() and swapcase(). Learn what each returns, when to use casefold(), and how to avoid title() pitfalls.
Concatenate strings in Python with +, f-strings or str.join. This guide shows when each works best and how to avoid the TypeError from adding a string and a number.
Python string slicing uses text[start:stop:step] to return part of a string. Learn indexes, negative values, steps, reversing and why slices never raise IndexError.
Use triple quotes for a Python multiline string, then fix indentation with textwrap.dedent or adjacent literals. Examples cover join, f-strings and raw text.
A Python string is an immutable str of Unicode characters. Learn how to create one, slice it, call common methods and format text with f-strings.
The walrus operator := assigns and returns a value in a single expression. See the loop, regex and comprehension patterns where it helps, and where Python forbids it.
Learn what &, |, ^, ~, << and >> do in Python, why ~5 is -6, how negative numbers shift, and the flag and masking patterns you will use in real code.
Use Decimal for money and exact decimal rules, float for science and speed. Learn string construction, quantize, rounding modes and contexts.
Use math.isclose instead of == for floats. Learn rel_tol, abs_tol, comparing with zero and how pytest.approx and NumPy defaults differ.
Python's int type has arbitrary precision, so large values never overflow. Learn how it is stored, what it costs and how the 4300-digit string limit works.