Strings cheat sheet
Working with text. Each example shows the code and exactly what it prints.
s = "Python" print(len(s), s[0], s[-1]) | 6 P n | Length and indexing |
s = "Python" print(s[1:4], s[::-1]) | yth nohtyP | Slicing and reversing |
print("hello".upper(), "HeLLo".lower()) | HELLO hello | Change case |
print(" pad ".strip() + "!") | pad! | Remove spaces at the ends |
print("a,b,c".split(",")) | ['a', 'b', 'c'] | Split into a list |
print("-".join(["a", "b", "c"])) | a-b-c | Join a list |
print("banana".count("a"), "banana".find("n")) | 3 2 | Count and find |
print("cat".replace("c", "b")) | bat | Replace |
print("py" in "python", "42".isdigit()) | True True | Membership and checks |
print("ab" * 3) | ababab | Repeat |
Py-CRAFT cheat sheet. © 2026 Dr. Saptarshi Das (PhD, IIT Kharagpur | ME, Jadavpur University). All rights reserved.