Part of Python: Zero to Warrior, a free Python course for beginners. Video: https://youtu.be/ghfy07d_L3o · Full course: https://www.youtube.com/playlist?list=PLcsbg964FAwc
- A method is a function that belongs to a value. You call it with a dot, its name and a pair of parentheses:
player.upper(). The parentheses are what run it. upper()andlower()give you the text in all capitals or all lowercase.replace(old, new)swaps one part of a string for another:"Warrior".replace("rior", "lock")is"Warlock".isupper()asks "are all the letters capitals?" andisdigit()asks "is every character a digit, 0 to 9?". Both give back a bool,TrueorFalse.isupper()only looks at letters:"LEVEL 7".isupper()isTrue. It needs at least one capital, though, so a string with no letters, like"42", givesFalse.isdigit()needs every character to be a digit:"42 gold","7.5"and"-7"are allFalse, and so is an empty string""(what you get if someone just hits Enter).
strip()takes the spaces off the start and the end of a string.int()copes with those spaces, butisdigit()doesn't, so strip what someone typed before you check it.split(",")cuts a string at every comma and gives you a list of the pieces, in order:['sword', 'shield', 'potion']. With empty parentheses,split()cuts at the spaces."-".join(items)glues a list back into one string, with the glue between the pieces:sword-shield-potion.
- Calling a method without saving the result:
name.upper()on its own line changes nothing, because strings are immutable (they can't be changed). Save the new string:name = name.upper(). - Forgetting the parentheses:
print(name.upper)prints something like<built-in method upper of str object at 0x1010db480>(the number is different every run) instead ofWARRIOR.
solution.py:methods.pyfrom the video.check.py: the second program from the video (type a level with a couple of spaces in front of it).starter.py: the two "your turn" exercises, with TODOs to fill in.
python3 solution.py # Mac
python solution.py # Windows
- Ask for a chat message with
input(), then print it in lowercase and print whether it was all capitals withisupper(). Run it twice: typeGG EZ, thengg ez. (The first saysTrue, the secondFalse.) - Make
date = "2026-10-18". Split it on the dashes and print the list, then join it back with"/"so it prints2026/10/18. - Predict first, then run:
print("7".isdigit()),print("-7".isdigit())andprint("007".isdigit()). Which one saysFalse, and why?