Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/marioaje/Python/llms.txt

Use this file to discover all available pages before exploring further.

Text is everywhere in software — user input, log files, configuration values, API responses. Python’s built-in re module gives you a powerful toolkit for searching, splitting, and transforming text using regular expressions: concise patterns that describe exactly what you are looking for. Whether you need to validate an email address, extract dates from a document, or clean up messy data, re handles it in a single function call. Alongside text processing, dictionaries and lists of dictionaries let you store structured, JSON-like data directly in Python — a skill that becomes essential the moment you start working with real-world APIs and databases.

Section 1 — Regular Expressions with the re Module

To use regular expressions you first import the re module. The course examples in expresionesregulares.py walk through the four most common operations — search, findall, split, and sub — on a single text string.

The source code in full

Semana 2/expresionesregulares.py
import re

txt = "Clase 19 Abril nuevas clases 2024"
# Impr normal
print(txt)

# Buscando palabras
buscardor = re.search("^Clase.*Abril", txt)
print(buscardor)

# Muestra las coincidencias que encuentra con la palabra
print(re.findall("se", txt))

# Split
print(re.split("\s", txt))

# replace data, remplaza el caracter buscado,
# por uno seleccionado por el usuario
print(re.sub("a", "--------------", txt))

evaluacion = re.search("^Clase.*Abril", txt)
print(evaluacion.span())

Walking through each operation

import re

txt = "Clase 19 Abril nuevas clases 2024"

# ^ anchors the match to the start of the string
# .* matches any characters in between
buscardor = re.search("^Clase.*Abril", txt)
print(buscardor)
# <re.Match object; span=(0, 13), match='Clase 19 Abril'>
re.search() returns a Match object when the pattern is found, or None when it is not. Always check the return value before calling .span() or any other method on it — calling a method on None raises an AttributeError. The safe pattern is:
evaluacion = re.search("^Clase.*Abril", txt)
if evaluacion:
    print(evaluacion.span())
else:
    print("Pattern not found")

Common re functions — quick reference

FunctionSignatureWhat it doesReturns
re.search()re.search(pattern, string)Scans through the string and returns the first location where the pattern matchesMatch object or None
re.findall()re.findall(pattern, string)Finds all non-overlapping matches of the patternlist of strings
re.split()re.split(pattern, string)Splits the string at every occurrence of the patternlist of substrings
re.sub()re.sub(pattern, repl, string)Replaces every match of the pattern with replNew string
re.match()re.match(pattern, string)Like search, but only matches at the start of the stringMatch object or None

Section 2 — Dictionaries and Nested Dictionaries

A Python dictionary stores key-value pairs and is the natural way to represent a structured record — much like a JSON object. lista.py shows how to build a flat dictionary, a standalone nested dictionary, and then embed one inside the other.
Semana 2/lista.py
# Esto es un arreglo de datos
mylista = [0, 1, 2, 3, 4, 5]

direccion = {
    'Direccion': "Costa Rica",
    'Provincia': 'SJ',
    'Canton': 'SJ',
    'Ciudad': 'Sabana'
}

diccionarioJson = {
    'nombre': 'Mario',
    'Apellido': 'Jimenez',
    'Telefono': '61383453',
    'MyLista': [0, 1, 2, 3, 4, 5],
    'Direccion': direccion          # nested dictionary
}

test = {
    "id": "20",
    "name": "Otro",
    "email": "Usuario@g.c",
    "password": "93dfcaf3d923ec47edb8580667473987"
}

print(test)

# print(diccionarioJson['nombre'])

# for keylista in diccionarioJson:
#     # print('Keylista: ', diccionarioJson[keylista])
Key points:
  • direccion is an ordinary flat dictionary representing an address.
  • diccionarioJson embeds direccion as the value for its 'Direccion' key — giving you a nested dictionary.
  • 'MyLista' shows that dictionary values can be any Python type, including lists.
  • test resembles a typical user record you might receive from a REST API — string IDs, emails, and hashed passwords all stored as key-value pairs.

Accessing values

# Top-level key
print(diccionarioJson['nombre'])        # Mario

# Nested key (two levels deep)
print(diccionarioJson['Direccion']['Ciudad'])   # Sabana

# Iterating over all keys
for keylista in diccionarioJson:
    print('Keylista: ', diccionarioJson[keylista])

Section 3 — Lists and Mixed-Type Dictionary Values

Python lists can hold any type of value, including dictionaries. The lista.py source shows this directly: mylista is a plain list of integers, and diccionarioJson embeds it as the value for the 'MyLista' key alongside strings and a nested dictionary.
Semana 2/lista.py
# Esto es un arreglo de datos
mylista = [0, 1, 2, 3, 4, 5]

# print(mylista)
When a list lives inside a dictionary, you access it the same way as any other value and can then index or iterate it normally:
# Access the embedded list
print(diccionarioJson['MyLista'])       # [0, 1, 2, 3, 4, 5]

# Iterate over the embedded list
for numero in diccionarioJson['MyLista']:
    print(numero)

# Access the nested Direccion dictionary
print(diccionarioJson['Direccion']['Ciudad'])   # Sabana
The test dictionary in the source file models a typical user record — the kind returned by a REST API or stored in a database:
Semana 2/lista.py
test = {
    "id": "20",
    "name": "Otro",
    "email": "Usuario@g.c",
    "password": "93dfcaf3d923ec47edb8580667473987"
}

print(test)
Notice that all values in test are strings, including "id" and "password". This mirrors how JSON data arrives from a REST API — even numbers are often transmitted as strings and must be converted with int() or float() before you can perform arithmetic on them.

Flat dictionary

Key-value pairs at a single level. Access values with dict['key'].

Nested dictionary

A dictionary whose value is another dictionary. Access with dict['outer']['inner'].

Embedded list

A dictionary value can be a list. Access with dict['key'] then index or iterate normally.

Mixed values

Dictionary values can be strings, numbers, lists, or other dictionaries — just like JSON.

Build docs developers (and LLMs) love