Skip to content

Instantly share code, notes, and snippets.

def func():
received = yield 'hello' # received is a generator
return received
print(list(func())) # prints ['hello']
Method/Attribue Purpose
split() Split the string into a list; splitting it whenever the RE matches
sub() Find all substrings where the RE matches and replace them with a different string
subn() Does the same thing as sub() but returns the new string and the number of replacements.
Metacharacters Description
| 'or' operator.
^ matches at the beginning of lines.
$ matches at the end of a line. Can be end of a string or any location followed by a new line.
\A matches only at the start of the string.
\z matches only at the end of the string.
\b this is a zero-width assertion that matches only at the beginning or end of a word.
\B this is a zero-width assertion and is opposite of \b
Long Flag Short Flag Meaning
ASCII A Makes several escapes like \w \b \s \d match only on ASCII characters.
DOTALL S Make . match any character including newlines.
IGNORECASE I Do case-insensitive matches.
LOCALE L Do a locale-aware match.
MULTILINE M Multi-line matching affecting ^ and $.
VERBOSE X (for 'extended') Enable verbose REs which can be organized more cleanly and understandably.
Regular String Raw String
'happy*' r'happy*'
'\\new' r'\new'
'\\w+\\s+' r'\w+\s+'
Characters Stage
'\new' Text string to be matched
'\\new' Escaped backslash for `re.compile()`
'\\\\new' Escaped backslashes for a string literal
from threading import Thread
from time import perf_counter
def sum_up_to(start: int, end: int):
total = 0
print('start...')
for i in range(start, end):
total += i
print('end...')
from threading import Thread
from time import perf_counter
def sum_up_to(start: int, end: int):
total = 0
print('start...')
for i in range(start, end):
total += i
print('end...')
import ctypes
import weakref
import gc
class Serval:
def __init__(self, name, cousin = None):
self.name = name
self.cousin = cousin
class Cat:
import weakref
import gc
import ctypes
class PriceyObject:
def __init__(self, name):
self.name = name
def __repr__(self):