R
Rishtaara
Python: Zero to Professional
Lesson 11 of 40Article18 min

RegEx, PIP & Try/Except

The re module matches patterns in text. re.search(), re.findall(), re.sub() are common.

Python RegEx

The re module matches patterns in text. re.search(), re.findall(), re.sub() are common.

Raw strings r"pattern" avoid backslash escaping headaches.

Real-life example: RegEx is a custom search filter — find all emails in a paragraph, not just one word.

Pattern matching
import re

text = "Contact: support@knowvora.com"
match = re.search(r"[\w.-]+@[\w.-]+", text)
print(match.group() if match else "No email")

Python PIP

pip installs third-party packages: pip install requests, pip list, pip show package.

Use python -m pip install package for reliability. Pin versions in requirements.txt for projects.

Real-life example: pip is an app store for Python — one command downloads tools others built.

PIP commands (terminal)
# Run in terminal — not inside .py file
# python -m pip install requests
# python -m pip list
# python -m pip freeze > requirements.txt

Python Try...Except

try/except catches errors so programs do not crash. Use specific exceptions like ValueError, FileNotFoundError.

else runs if no error; finally always runs (cleanup).

Real-life example: try/except is a safety net under a trapeze — the show continues even if one move fails.

Handle conversion errors
try:
    score = int("abc")
except ValueError:
    print("Invalid number")
finally:
    print("Attempt finished")