What is the use of the ascii function in Python?
ascii() works like repr() but escapes every non-ASCII character as \x, \u or \U. See how it differs from repr(), str() and encode().
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
ascii() works like repr() but escapes every non-ASCII character as \x, \u or \U. See how it differs from repr(), str() and encode().
Python's str.format() fills placeholders with values and formats numbers, padding and dates. See syntax, specifiers and how it compares with f-strings.
all() checks that every element of an iterable is truthy and stops at the first failure. See syntax, truthiness rules, examples and pitfalls.
any() checks whether at least one element of an iterable is truthy and stops at the first match. See syntax, examples and common mistakes.
How Python's built-in max() returns the largest item, how to customize it with key and default, and the mistakes to avoid.
How Python's built-in min() returns the smallest item, how key and default change its behavior, and the mistakes to avoid.
Python zip() pairs items from several iterables into tuples. See parallel loops, strict mode, zip_longest, unzipping and dict creation with examples.
A list is an ordered sequence read by position; a dictionary maps unique keys to values. See syntax, speed and use cases side by side.
Python filter() lazily keeps the items of an iterable that pass a test. See the syntax, lambdas, filter(None), and when a comprehension is better.
Python map() applies a function to every item of an iterable and returns a lazy iterator. See lambdas, multiple iterables and map versus comprehensions.
map() transforms every item and filter() selects items by a condition. Compare them with Python 3 examples, lazy behaviour and comprehension alternatives.
starmap() calls a function with each tuple of arguments unpacked, like map() for pre-grouped data. See examples, the map comparison and the multiprocessing variant.
itertools.repeat returns an iterator that yields the same value again and again. See how it works with map, zip and loops, and why mutable values are shared.
itertools.tee creates independent iterators from one iterable. Learn how it buffers items, when it helps and when a list or pairwise is better.
combinations_with_replacement yields every unordered selection of r items where repeats are allowed. See syntax, practical examples, counts and how it differs from combinations.
itertools.combinations lazily generates every unordered selection of r items from an iterable. See syntax, examples, performance limits and how it differs from permutations.
itertools.product yields every combination of items across iterables as tuples. See nested-loop replacement, the repeat argument and edge cases.
itertools.permutations lazily yields every ordering of items as tuples. See r, string examples, duplicate handling and how fast the count grows.
islice slices any Python iterable lazily, using start, stop and step like list slicing. Learn when to use it, how it consumes iterators, and what to use for chunking.
zip_longest combines iterables until the longest one ends, padding shorter ones with a fill value. See syntax, examples and how it differs from zip().