python_introduction/examples/string_format.py

41 lines
610 B
Python

# Basic placeholders usage
ingredient = "sugar"
quantity = "500g"
ingredient_string = "- {}: {}".format(ingredient, quantity)
print(ingredient_string)
# 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
fname = "John"
lname = "Doe"
bond_string = "My name is {lastname}, {firstname} {lastname}".format(firstname=fname, lastname=lname)
print(bond_string)