Working with Strings

Strings and String Operations

Part of: Variables, Lists & Procedures

A string is a sequence of characters, text like "Hello", "42", or even an empty "". Strings are how programs handle names, messages, and any data made of letters and symbols. Strings are ordered like lists. Characters in a string have indexes , exactly like list elements. Zero-indexed, with negative indexes counting from the end: Because they are ordered sequences, you can traverse a string with a loop: Common string operations. - Concatenation with + joins strings: "Ada" + " " + "Lovelace" → "Ada Lovelace". - Repetition with : "ab" 3 → "ababab". - Length : len(s) counts characters. - Case : s.upper() and s.lower() return new strings; the original is unchanged. - Slicing : s[1:4] takes characters 1 up to (not including) 4. s[::-1] reverses the whole string. Strings are immutable. You cannot change a character in place: name[0] = "A" raises an error. Instead you build a new string . To count or transform characters, loop and accumulate: What usually goes wrong. What you see What caused it How to fix it --- --- --- TypeError: 'str' object does not support item assignment A character was written into an existing string in place Strings cannot be edited, so build a new string from the

Challenge: Reverse and Count Vowels