Advanced String Operations

Python StringsFree Lesson

Advertisement

Advanced String Operations

String parsing, regex patterns, and text processing.

Overview

Master advanced string techniques.

Regular Expressions

import re

text = "Contact us at support@example.com or sales@company.org"

# Find emails
emails = re.findall(r'[\w.+-]+@[\w-]+\.[\w.]+', text)
print(emails)  # ['support@example.com', 'sales@company.org']

# Validate phone number
phone_pattern = r'^\+?1?\d{9,15}$'
print(bool(re.match(phone_pattern, "+1234567890")))  # True

# Extract data
date_text = "Event on 2024-01-15 at 10:30"
match = re.search(r'(\d{4})-(\d{2})-(\d{2})', date_text)
if match:
    year, month, day = match.groups()
    print(f"{year}-{month}-{day}")

String Methods

text = "  Hello, World!  "

# Advanced methods
print(text.strip())           # "Hello, World!"
print(text.center(20, '-'))   # "--Hello, World!---"
print(text.replace("World", "Python"))  # "  Hello, Python!  "
print(text.split())           # ['Hello,', 'World!']

# Case conversion
print(text.upper())           # "  HELLO, WORLD!  "
print(text.title())           # "  Hello, World!  "
print(text.swapcase())        # "  hELLO, wORLD!  "

# Checking
print(text.isalnum())         # False
print(text.istitle())         # False
print("hello".isalpha())      # True
print("123".isdigit())        # True

Text Processing

def extract_domains(text):
    pattern = r'@([\w.-]+)'
    return re.findall(pattern, text)

def clean_text(text):
    # Remove extra whitespace
    text = re.sub(r'\s+', ' ', text)
    # Remove special characters
    text = re.sub(r'[^\w\s]', '', text)
    return text.strip()

# Usage
emails = "Contact alice@gmail.com or bob@yahoo.com"
print(extract_domains(emails))  # ['gmail.com', 'yahoo.com']

Practice

Build a text analyzer that extracts insights from documents.

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement