What is the use of the "as" keyword in Python?
The as keyword binds a new name in imports, except clauses, with statements and match patterns. See each use with examples and scoping rules.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
The as keyword binds a new name in imports, except clauses, with statements and match patterns. See each use with examples and scoping rules.
import loads a module once and binds its names in your code. See import forms, packages, virtual environments, relative imports and common errors.
A module is a Python file whose functions and classes you reuse with import. Learn import forms, sys.path, packages and common mistakes.
The with statement guarantees cleanup of files, locks and connections, even when errors occur. See how it works and how to write your own context manager.
close() flushes buffers and releases the file handle. See why the with statement is safer, what happens after closing, and common file mistakes.
The read() method returns a file's content as a string or bytes. See full and partial reads, binary mode, large-file patterns and how it differs from readline().
Learn how file.write() works in Python: file modes, newlines, return value, binary data, buffering and the mistakes that overwrite or lose data.
Python open() returns a file object for reading and writing. Learn the file modes, why to use with, how to set encoding, and how to handle errors.
A Python file is an object from open() for reading and writing text or bytes. See modes, with blocks, pathlib and common mistakes, with examples.
The re module is Python's regular expression library for finding, validating, extracting and replacing text patterns. See the key functions, groups, flags and pitfalls.
A practical guide to Python regex: the re functions, pattern syntax, named groups, flags, compiled patterns and how to avoid common mistakes.
str.join() combines the items of an iterable into one string using a separator. Learn syntax, how to handle numbers, why it beats += in loops, and real examples.
str.replace() returns a new string with a substring swapped for another. See how count works, how to ignore case and when translate or re.sub fits better.
str.split() breaks a string into a list on whitespace or a separator. See maxsplit, the split() versus split(' ') trap, rsplit, splitlines and regex splitting.
Compare a string with its reverse using slicing, a two-pointer loop, or a normalised check that ignores case, spaces and punctuation.
Python slicing extracts parts of lists, strings, and tuples with start, stop, and step. Learn negative indices, reversing, copies, and slice assignment.
Sort a Python list in place with sort() or get a new list with sorted(). Covers key functions, reverse order, tuples, dictionaries and stable multi-field sorting.
How Python's not in membership operator works on lists, sets, dictionaries and strings, with performance tips and common mistakes.
Learn how the Python in keyword tests membership in lists, sets, dictionaries and strings, powers for loops, and how __contains__ customises it.
A Python set stores unique, hashable items and supports fast membership tests and set algebra. Learn the syntax, operations and common pitfalls.