From c38d82ff3c910d84e5fa372c547c5f9b2277dbb8 Mon Sep 17 00:00:00 2001 From: Steve De Jongh Date: Thu, 28 Oct 2021 14:47:49 +0200 Subject: [PATCH] Added indexed placeholders sample --- examples/string_format.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/examples/string_format.py b/examples/string_format.py index 554a100..2ec6f87 100644 --- a/examples/string_format.py +++ b/examples/string_format.py @@ -1,4 +1,4 @@ -# Basic placeholders +# Basic placeholders usage ingredient = "sugar" quantity = "500g" @@ -8,7 +8,24 @@ ingredient_string = "- {}: {}".format(ingredient, quantity) print(ingredient_string) -# Named placeholders + + +# 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" @@ -16,3 +33,8 @@ lname = "Doe" bond_string = "My name is {lastname}, {firstname} {lastname}".format(firstname=fname, lastname=lname) print(bond_string) + + + + +