Learn Without Walls
← Back to Module 7

Module 7: Strings in Depth -- Quick Reference

A printable reference card covering string methods, slicing, searching, and common patterns.

Case Conversion

MethodDescriptionExample
.upper()All uppercase"hello".upper() → "HELLO"
.lower()All lowercase"HELLO".lower() → "hello"
.title()Capitalize each word"hello world".title() → "Hello World"
.capitalize()Capitalize first letter"hello world".capitalize() → "Hello world"
.swapcase()Swap upper/lower"Hello".swapcase() → "hELLO"

Whitespace and Stripping

MethodDescriptionExample
.strip()Remove both ends" hi ".strip() → "hi"
.lstrip()Remove left side" hi ".lstrip() → "hi "
.rstrip()Remove right side" hi ".rstrip() → " hi"
.strip(chars)Strip specific chars"###hi###".strip("#") → "hi"

Searching

MethodDescriptionNot Found
.find(sub)Index of first occurrenceReturns -1
.rfind(sub)Index of last occurrenceReturns -1
.index(sub)Index of first occurrenceRaises ValueError
.rindex(sub)Index of last occurrenceRaises ValueError
sub in sCheck existenceReturns False
.count(sub)Count occurrencesReturns 0

Replacing and Transforming

MethodDescriptionExample
.replace(old, new)Replace all occurrences"aab".replace("a","x") → "xxb"
.replace(old, new, n)Replace first n"aab".replace("a","x",1) → "xab"

Checking Methods

MethodReturns True When
.startswith(sub)String starts with sub
.endswith(sub)String ends with sub
.isdigit()All characters are digits
.isalpha()All characters are letters
.isalnum()All characters are letters or digits
.islower()All cased characters are lowercase
.isupper()All cased characters are uppercase
.isspace()All characters are whitespace

String Slicing

SyntaxDescriptionExample (s = "Python")
s[a:b]From index a to b-1s[0:3] → "Pyt"
s[:b]From start to b-1s[:2] → "Py"
s[a:]From a to ends[4:] → "on"
s[-n:]Last n characterss[-3:] → "hon"
s[::2]Every other characters[::2] → "Pto"
s[::-1]Reverseds[::-1] → "nohtyP"

Splitting and Joining

MethodDescriptionExample
.split()Split on whitespace"a b c".split() → ["a","b","c"]
.split(sep)Split on separator"a,b,c".split(",") → ["a","b","c"]
.split(sep, n)Split max n times"a,b,c".split(",",1) → ["a","b,c"]
.splitlines()Split on line breaks"a\nb".splitlines() → ["a","b"]
sep.join(list)Join list with separator"-".join(["a","b"]) → "a-b"

Special Strings

TypeSyntaxDescription
f-stringf"Hello {name}"Formatted string with embedded expressions
Raw stringr"C:\path"Backslashes treated literally
Multiline"""text"""String spanning multiple lines

Escape Characters

EscapeMeaning
\nNewline
\tTab
\\Literal backslash
\"Literal double quote
\'Literal single quote