Overview of Common Python 3 Standard Library Modules with Examples
This article presents a concise, categorized guide to frequently used Python 3 standard library modules—including text processing, file handling, networking, data manipulation, concurrency, time/date, mathematics, and system utilities—each accompanied by clear explanations and runnable code snippets.
Python provides a rich standard library that can be used without any additional installation. The following guide classifies and demonstrates some of the most commonly used modules.
1. Text processing
re (regular expressions) – Offers pattern matching, searching and substitution functions.
import re
text = "Hello, World! 123"
pattern = r"\d+" # match digits
matches = re.findall(pattern, text)
print(matches) # Output: ['123']string – Provides constants and utility functions for string manipulation.
import string
all_chars = string.ascii_letters + string.digits
print(all_chars)difflib – Compares sequences and produces human‑readable difference reports.
import difflib
text1 = "Hello, World!"
text2 = "Hello, Python!"
differ = difflib.Differ()
diff = differ.compare(text1, text2)
print('
'.join(diff))textwrap – Formats and wraps long strings to a given width.
import textwrap
text = "This is a long text that needs to be wrapped to fit within a certain width."
wrapped = textwrap.wrap(text, width=20)
print(wrapped)codecs – Handles character encoding and decoding.
import codecs
text = "你好,世界!"
encoded = codecs.encode(text, "utf-8")
decoded = codecs.decode(encoded, "utf-8")
print(decoded)2. File and directory operations
os – Low‑level OS interfaces such as cwd, listdir, mkdir, etc.
import os
cwd = os.getcwd()
print(cwd)
files = os.listdir('.')
print(files)shutil – High‑level file operations like copy, move, rmtree.
import shutil
shutil.copy('source.txt', 'destination.txt')
shutil.move('source.txt', 'destination.txt')
shutil.rmtree('directory')pathlib – Object‑oriented path manipulation.
from pathlib import Path
path = Path('/path/to/file.txt')
print(path.exists())
print(path.parent)
content = path.read_text()
print(content)fileinput – Iterates over lines from one or more files.
import fileinput
for line in fileinput.input('file.txt'):
print(line)tempfile – Creates temporary files and directories.
import tempfile
with tempfile.NamedTemporaryFile() as temp_file:
print(temp_file.name)
with tempfile.TemporaryDirectory() as temp_dir:
print(temp_dir)3. Network and Web
urllib – Basic URL handling and HTTP requests.
from urllib import request
response = request.urlopen('https://www.example.com')
content = response.read()
print(content)http.client – Low‑level HTTP client.
import http.client
conn = http.client.HTTPSConnection('www.example.com')
conn.request('GET', '/')
response = conn.getresponse()
content = response.read()
print(content)smtplib – Sends email via SMTP.
import smtplib
from email.mime.text import MIMEText
msg = MIMEText('This is a test email.')
msg['Subject'] = 'Test Email'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
with smtplib.SMTP('smtp.example.com') as server:
server.sendmail('[email protected]', '[email protected]', msg.as_string())ftplib – FTP client for uploading/downloading files.
from ftplib import FTP
ftp = FTP('ftp.example.com')
ftp.login('username', 'password')
ftp.cwd('/path/to/directory')
ftp.retrbinary('RETR file.txt', open('file.txt', 'wb').write)
ftp.quit()socket – Low‑level network communication.
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 8000))
server_socket.listen(1)
while True:
client_socket, address = server_socket.accept()
data = client_socket.recv(1024)
client_socket.sendall(b'Hello, client!')
client_socket.close()4. Data handling
json – Encode/decode JSON data.
import json
data = {'name': 'John', 'age': 30}
json_str = json.dumps(data)
print(json_str)
decoded_data = json.loads(json_str)
print(decoded_data)csv – Read/write CSV files.
import csv
with open('data.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
with open('output.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Age'])
writer.writerow(['John', 30])sqlite3 – SQLite database access.
import sqlite3
conn = sqlite3.connect('example.db')
conn.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL)''')
conn.execute("INSERT INTO users (name, age) VALUES ('John', 30)")
conn.commit()
cursor = conn.execute('SELECT * FROM users')
for row in cursor:
print(row)
conn.close()collections – Advanced data structures such as namedtuple and Counter.
from collections import namedtuple, Counter
Person = namedtuple('Person', ['name', 'age'])
person1 = Person('John', 30)
print(person1.name)
print(person1.age)
data = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
counter = Counter(data)
print(counter['apple'])
print(counter.most_common(2))itertools – Functions for creating and combining iterators.
import itertools
counter = itertools.count(start=1, step=2)
print(next(counter))
print(next(counter))
print(next(counter))
repeater = itertools.repeat('Hello', times=3)
print(next(repeater))
print(next(repeater))
print(next(repeater))
combinations = itertools.combinations([1, 2, 3], 2)
print(list(combinations))5. Concurrency and parallelism
threading – Multithreaded programming.
import threading
def worker():
print("Worker thread")
thread = threading.Thread(target=worker)
thread.start()multiprocessing – Multiprocess programming.
import multiprocessing
def worker():
print("Worker process")
process = multiprocessing.Process(target=worker)
process.start()concurrent.futures – High‑level concurrent execution.
import concurrent.futures
def task():
print("Task executed")
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.submit(task)asyncio – Asynchronous I/O framework.
import asyncio
async def task():
print("Task executed")
loop = asyncio.get_event_loop()
loop.run_until_complete(task())queue – Thread‑safe queues.
import queue
q = queue.Queue()
q.put("Data 1")
q.put("Data 2")
data = q.get()
print(data)6. Time and date
datetime – Date and time manipulation.
from datetime import datetime, timedelta
current_datetime = datetime.now()
print(current_datetime)
formatted = current_datetime.strftime('%Y-%m-%d %H:%M:%S')
print(formatted)
future = current_datetime + timedelta(days=7)
print(future - current_datetime)time – Time‑related functions.
import time
current_timestamp = time.time()
print(current_timestamp)
time.sleep(1)
current_time = time.localtime()
print(current_time)calendar – Calendar generation and queries.
import calendar
cal = calendar.calendar(2023)
print(cal)
print(calendar.isleap(2023))
print(calendar.month(2023, 6))timeit – Precise performance measurement.
import timeit
execution_time = timeit.timeit('sum(range(1, 1000))', number=10000)
print(execution_time)7. Mathematics and scientific computing
math – Mathematical functions.
import math
print(math.sqrt(16))
print(math.sin(math.pi/2))
print(math.log(math.e))random – Random number generation.
import random
print(random.randint(1, 10))
print(random.uniform(0, 1))
my_list = [1, 2, 3, 4, 5]
print(random.choice(my_list))statistics – Basic statistical functions.
import statistics
print(statistics.mean([1, 2, 3, 4, 5]))
print(statistics.median([1, 2, 3, 4, 5]))
print(statistics.variance([1, 2, 3, 4, 5]))fractions – Rational number arithmetic.
from fractions import Fraction
print(Fraction(1, 2) + Fraction(1, 3))
print(Fraction(3, 4) * Fraction(2, 5))
print(Fraction(2, 3) / Fraction(1, 4))decimal – Decimal fixed‑point arithmetic.
from decimal import Decimal
print(Decimal('0.1') + Decimal('0.2'))
print(Decimal('2.5') * Decimal('3.5'))
print(Decimal('10') / Decimal('3'))8. System and environment
sys – Interpreter and system information.
import sys
print(sys.version)
print(sys.argv)
sys.setrecursionlimit(1000)platform – Platform‑specific details.
import platform
print(platform.system())
print(platform.node())
print(platform.python_version())os.path – Path manipulation utilities.
import os.path
print(os.path.exists('/path/to/file'))
print(os.path.basename('/path/to/file.txt'))
print(os.path.join('/path/to', 'file.txt'))argparse – Command‑line argument parsing.
import argparse
parser = argparse.ArgumentParser(description='A simple command-line tool.')
parser.add_argument('--input', help='Input file path')
parser.add_argument('--output', help='Output file path')
args = parser.parse_args()
print(args.input)
print(args.output)logging – Flexible logging facility.
import logging
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler('app.log')
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')These examples illustrate how the Python 3 standard library can be leveraged to perform a wide range of common programming tasks without installing third‑party packages.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
