![]() |
|
CHAPTER 6 — STRINGS - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Courses — Structured Learning (https://theluminarchive.co.uk/forumdisplay.php?fid=69) +---- Forum: Beginner’s Guide to Coding — Python From Zero to Skill (https://theluminarchive.co.uk/forumdisplay.php?fid=72) +---- Thread: CHAPTER 6 — STRINGS (/showthread.php?tid=224) |
CHAPTER 6 — STRINGS - Leejohnston - 11-15-2025 Chapter 6 — Strings Understanding and controlling text in Python A string is simply text inside quotation marks. Strings are used for: • names • messages • sentences • user input • filenames • any text your program displays Python gives you powerful tools to create and manipulate strings. --- 6.1 Creating Strings Strings always go inside quotes: Code: name = "Johno"You can use: • double quotes " " • or single quotes ' ' Both are fine: Code: word1 = "hello"--- 6.2 Printing Strings Simple: Code: print("Hello there!")Or using variables: Code: name = "Mia"--- 6.3 Combining Strings (Concatenation) Use + to join strings: Code: first = "Hello"Output: Code: Hello worldRemember: You can only add strings to strings. This fails: Code: age = 16Fix using str(): Code: age = 16--- 6.4 Repeating Strings Python lets you multiply a string: Code: print("ha" * 3)Output: Code: hahahaUseful for patterns, dividers, or simple banners. --- 6.5 Escape Characters (Special Symbols) Sometimes you need quotation marks *inside* your string. Python uses backslashes to escape them: Code: print("He said \"Hello!\"")Common escape codes: • \n — new line • \t — tab • \" — quote • \\ — backslash Example: Code: print("Line 1\nLine 2")--- 6.6 String Length Use len() to measure the number of characters: Code: word = "Python"Output: Code: 6Counting includes: • spaces • letters • numbers • punctuation --- 6.7 Indexing — Getting Individual Characters Each character has a position: Code: text = "hello"Important: • Python starts counting at 0 • Last character = index -1 Example: Code: print(text[-1]) # o--- 6.8 Slicing — Getting Parts of a String Format: Code: string[start:end]Example: Code: word = "Python"Start is included. End is NOT included. Shortcut examples: Code: print(word[:3]) # from start to position 3--- 6.9 Changing Case Python has built-in string functions: Code: message = "hello world"--- 6.10 Removing Spaces Stripping spaces from ends: Code: text = " hello "Other options: • .lstrip() — left only • .rstrip() — right only --- 6.11 Finding Substrings Check if part of a string exists: Code: sentence = "Python is powerful"--- 6.12 Replacing Parts of a String Code: text = "I like apples"--- 6.13 Exercise — Sentence Tools Create a file called string_tools.py. Ask the user for a sentence. Then print: 1. The length of the sentence 2. The first character 3. The last character 4. The sentence in uppercase 5. The sentence reversed (Hint: reversed string = Code: text[::-1]--- 6.14 Small Challenge Ask the user for: • their first name • their last name Then output: Code: "Initials: F.L."(Hint: first character of each name) --- 6.15 Chapter Summary • Strings store text • Use + to combine strings • Use * to repeat strings • Escape characters allow quotes, new lines, etc. • len() counts characters • Indexing gets single letters • Slicing gets parts of a string • .upper(), .lower(), .title() change case • .strip() removes spaces • .replace() replaces text Next: Chapter 7 — Numbers & Maths in Python Now your programs begin to calculate real values. --- Written and Compiled by Lee Johnston — Founder of The Lumin Archive |