python_introduction/examples/string_format.py

41 lines
610 B
Python
Raw Permalink Normal View History

2021-10-28 14:47:49 +02:00
# Basic placeholders usage
2021-10-28 09:16:46 +02:00
ingredient = "sugar"
quantity = "500g"
ingredient_string = "- {}: {}".format(ingredient, quantity)
print(ingredient_string)
2021-10-28 14:47:49 +02:00
# Indexed placeholders usage
#
# Placeholders are identified by index number, 0 being the first parameter passed
name = "John"
age = 30
greetings = "My name is {0}, i'm {1}.".format(name, age)
# Named placeholders usage
#
# Placeholders are identified by name, order doesn't matter
2021-10-28 09:16:46 +02:00
2021-10-27 20:11:34 +02:00
fname = "John"
lname = "Doe"
bond_string = "My name is {lastname}, {firstname} {lastname}".format(firstname=fname, lastname=lname)
print(bond_string)
2021-10-28 14:47:49 +02:00