Python Functions with Multi-Valued Parameters

Python Functions with Multi-Valued Parameters

str.strip() takes a string that is treated as a set of characters.

>>> "  Hello world! ".strip(" !")
'Hello world'

str.startswith and str.endswith accept a tuple of values.

>>> files = ["a/file1.md", "b/file2.py", "c/file3.txt"]
>>> [f for f in files if f.startswith(("a/", "b/"))]
['a/file1.md', 'b/file2.py']
>>> [f for f in files if f.endswith(("md", "txt"))]
['a/file1.md', 'c/file3.txt']

re.split takes a string that is actually a regex pattern.

>>> import re
>>> files = ["a/file1.md", "b/file2.py", "c/file3.txt"]
>>> [re.split("\/|\.",f)[-2] for f in files]
['file1', 'file2', 'file3']

Last modified on 2025-12-31