Datatyper i Python
Jason Myers
Instructor
f""cookie_name = "Anzac"
cookie_price = "$1.99"
print(f"Each { cookie_name } cookie costs { cookie_price }.")
"Each Anzac cookie costs $1.99."
"".join() använder strängen den anropas på för att sammanfoga ett iterabelt objektchild_ages = ["3", "4", "7", "8"]
print(", ".join(child_ages))
"3, 4, 7, 8"
print(f"The children are ages {','.join(child_ages[0:3])}, and {child_ages[-1]}.")
"The children are ages 3, 4, 7, and 8."
.startswith() och .endswith() kontrollerar om en sträng börjar eller slutar med ett visst tecken eller en viss strängboy_names = ["Mohamed", "Youssef", "Ahmed"]
print([name for name in boy_names if name.startswith('A')])
["Ahmed"]
in söker efter ett värde i ett iterabelt objekt, till exempel en sträng."long" in "Life is a long lesson in humility."
True
"life" in "Life is a long lesson in humility."
False
.lower() returnerar en sträng med enbart gemener"life" in "Life is a long lesson in humility.".lower()
True
Datatyper i Python