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