Case

"Hello".upper()       # HELLO
"Hello".lower()       # hello
"hello world".title() # Hello World
"Hello".swapcase()    # hELLO

Whitespace

"  hi  ".strip()   # 'hi'
"  hi  ".lstrip()
"  hi  ".rstrip()

Splitting and Joining

"a,b,c".split(",")       # ['a','b','c']
"one two".split()        # ['one','two']
",".join(["a","b","c"])  # 'a,b,c'
"line1\nline2".splitlines()

Searching

"hello".find("l")     # 2
"hello".rfind("l")    # 3
"hello".index("l")    # 2 (errors if missing)
"hello".startswith("he")
"hello".endswith("lo")
"hello".count("l")    # 2

Replacing

"banana".replace("a", "o")        # bonono
"banana".replace("a", "o", 1)     # bonana

Checks

"123".isdigit()
"abc".isalpha()
"abc123".isalnum()
" ".isspace()

Practise this on PyForm — free

PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.

Open PyForm →