1.8 String Operations

Loading content...

1.8 String Operations

Module 1.8 — String Operations | Python for ML 🐍 Module 8 â€ĸ Python for Machine Learning Master text manipulation — the skill behind every NLP, data cleaning, and web scraping task in Machine Learning. đŸŽ¯ Daily-life analogy: Imagine a friendship bracelet made of letter beads đŸ“ŋ. Each bead holds one character. A string is exactly that — a chain of characters You can look at any bead (indexing), cut a section (slicing), or read the whole bracelet — but you cannot swap a bead without making a brand-new bracelet. That's immutability! 🔒 📖 String: An ordered, immutable sequence of characters enclosed in quotes. Characters can be letters, digits, symbols, or spaces. Python type: str. 💡 In ML, almost all raw data starts as text — CSV files, tweets, emails, reviews. Strings are the gateway to turning messy human language into clean model-ready data. 🧠 🔗 A String = Chain of Character Blocks Each block = one character. The number = its index. Index starts at 0, not 1! đŸŽ¯ ✅ True or False — Activity 1 / 16 A Python string can be changed in place (mutable). âœī¸ Creating Strings — Three Ways Python gives you three quote styles to create strings. Pick whichever fits your text! 🎨 # Three ways to create strings a = 'Hello' # single quotes b = "World" # double quotes string""" # triple quotes (keeps line breaks) 💡 Tip: Single and double quotes produce the same string. Use double quotes when text contains an apostrophe: "it's easy". Use triple quotes for paragraphs or docstrings. 📝 🧠 Quick Check — Activity 2 / 16 Which quote style creates a multi-line string without \n? đŸ”ĸ String Indexing & Slicing đŸŽ¯ Analogy: Think of numbered mailboxes đŸ“Ŧ. You open one mailbox (indexing) or grab a range (slicing). 📍 Indexing — Pick One Character Use [] with a position number. -2 = second-to-last. Just counting from the right! â†Šī¸ âœ‚ī¸ Slicing — Cut a Substring Syntax: text[start : stop : step] stop is exclusive — goes up to but does NOT include it. âš ī¸ print( text[0:3] ) # PYT (index 0, 1, 2) print( text[2:] ) # THON (index 2 → end) print( text[:4] ) # PYTH (start → index 3) print( text[::-1] ) # NOHTYP (reversed!) print( text[::2] ) # PTO (every 2nd char) âœī¸ Fill in the Blank — Activity 3 / 16 Given word = "MACHINE", what does word[1:4] return? 🧠 Quick Check — Activity 4 / 16 What does "HELLO"[-2] return? âš™ī¸ Essential String Methods đŸŽ¯ Analogy: String methods are kitchen tools đŸŗ — upper() is the CAPS-LOCK key, strip() trims edges, split() chops text into pieces, and join() glues pieces back with any separator. 🔧 📌 Key rule: Strings are immutable. Every method returns a new string — the original stays untouched! You must save the result. 🔒 First letter of each word capitalized Remove leading/trailing whitespace Split into list at separator Join list items with separator email = " User@Gmail.COM " clean = email.strip().lower() # âœ‚ī¸ Split sentence into words sentence = "Machine Learning is fun" words = sentence.split() # → ['Machine', 'Learning', 'is', 'fun'] # 🔗 Join words with underscores # → "Machine_Learning_is_fun" ['Machine', 'Learning', 'is', 'fun'] 🧠 Quick Check — Activity 5 / 16 What does " Python ".strip() return? âœī¸ Fill in the Blank — Activity 6 / 16 To split "85,90,72" into ['85','90','72'], write: "85,90,72".split(____) 🔍 Searching Inside Strings đŸŽ¯ Analogy: Like Ctrl+F in a document — find() tells where, count() tells

Subject: Python for Machine Learning | Chapter: Chapter 1: Python Essentials

1.7 Dictionaries and Sets Chapter 1: Python Essentials 1.9 File Handling (read, write CSV)
Topics
Chapter 1: Python Essentials
Chapter 2: Python for Data Science
© Copyright Š 2020 ALGOPK . All Rights Reserved.