Python Cheat Sheet
Comprehensive reference for Python 3. Standard Library, Strings, Math, and System commands.
Resources
String Operations
String Methods
- capitalize()
- center(width, fillchar)
- count(sub, start, end)
- decode()
- encode(encoding, errors)
- endswith(suffix)
- expandtabs(tabsize)
- find(sub)
- format(*args)
- index(sub)
- isalnum()
- isalpha()
- isdigit()
- islower()
- isspace()
- istitle()
- isupper()
- join(iterable)
- ljust(width)
- lower()
- lstrip(chars)
- partition(sep)
- replace(old, new)
- rfind(sub)
- rindex(sub)
- rjust(width)
- rpartition(sep)
- rsplit(sep)
- rstrip(chars)
- split(sep)
- splitlines()
- startswith(prefix)
- strip(chars)
- swapcase()
- title()
- translate(table)
- upper()
- zfill(width)
- isnumeric()
- isdecimal()
String Formatting (%)
| Code | Description |
|---|---|
| %d / %i | Signed integer decimal |
| %o | Signed octal value |
| %u | Obsolete (same as %d) |
| %x | Hexadecimal (lowercase) |
| %X | Hexadecimal (uppercase) |
| %e | Exponential (lowercase) |
| %E | Exponential (uppercase) |
| %f | Floating point decimal |
| %F | Floating point decimal |
| %g | Floating point format |
| %c | Single character |
| %r | String (using repr()) |
| %s | String (using str()) |
| %% | Literal '%' character |
Date Formatting (strftime)
| Code | Description |
|---|---|
| %a | Abbreviated weekday (Sun) |
| %A | Weekday (Sunday) |
| %b | Abbreviated month (Jan) |
| %B | Month name (January) |
| %c | Date and time |
| %d | Day (01 to 31) |
| %H | 24 hour (00 to 23) |
| %I | 12 hour (01 to 12) |
| %j | Day of year (001-366) |
| %m | Month (01 to 12) |
| %M | Minute (00 to 59) |
| %p | AM or PM |
| %S | Second (00 to 61) |
| %U | Week number |
| %w | Weekday (0=Sun) |
| %x | Date representation |
| %X | Time representation |
| %y | Year (00-99) |
| %Y | Year (2025) |
| %Z | Time zone |
File System
Containers (Lists, Sets, Arrays)
Array / List Indexing
Example: a = [0, 1, 2, 3, 4, 5]
| Code | Result |
|---|---|
len(a) |
6 |
a[0] |
0 |
a[5] |
5 |
a[-1] |
5 (Last) |
a[-2] |
4 |
a[1:] |
[1, 2, 3, 4, 5] |
a[:5] |
[0, 1, 2, 3, 4] |
a[:-2] |
[0, 1, 2, 3] |
a[1:3] |
[1, 2] |
b = a[:] |
Shallow Copy |
Set Types
- len(s)
- x in s
- isdisjoint()
- issubset()
- union()
- intersection()
- difference()
- add(elem)
- remove()
- pop()
- clear()
Date & Time
Datetime Object
Time Object
Math & Random
Trigonometry
Sys & OS
Sys Variables
- argv Command line args
- path Search path
- platform Current platform
- stdin/out I/O Objects
- version Python version
- exit() Exit function
OS Variables
- name Name of OS
- sep Path separator
- getcwd() Current Dir
- listdir() List files
- mkdir() Make dir
- remove() Delete file