From 0654a19b44b290828211372e8148338c4c1d1c23 Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Tue, 15 Jul 2014 13:30:27 +0930 Subject: [python3/en] Clean up confusion between % and .format() --- python3.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python3.html.markdown b/python3.html.markdown index 7657295d..43d62e77 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -292,7 +292,7 @@ prints: mouse is a mammal """ for animal in ["dog", "cat", "mouse"]: - # You can use % to interpolate formatted strings + # You can use format() to interpolate formatted strings print("{} is a mammal".format(animal)) """ @@ -471,7 +471,7 @@ class Human(object): # An instance method. All methods take "self" as the first argument def say(self, msg): - return "{name}: {message}" % (name=self.name, message=msg) + return "{name}: {message}".format(name=self.name, message=msg) # A class method is shared among all instances # They are called with the calling class as the first argument -- cgit v1.2.3 From 164cf1ce78bcd96fda2a02addc0217ecdf033b5d Mon Sep 17 00:00:00 2001 From: Adam Brenecki Date: Wed, 16 Jul 2014 10:25:03 +0930 Subject: Mention that Python 2.5-style string formatting still works in 3 --- python3.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python3.html.markdown b/python3.html.markdown index 43d62e77..bc0c05bd 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -98,6 +98,10 @@ not False # => True # You can use keywords if you don't want to count. "{name} wants to eat {food}".format(name="Bob", food="lasagna") #=> "Bob wants to eat lasagna" +# If your Python 3 code also needs to run on Python 2.5 and below, you can also +# still use the old style of formatting: +"%s can be %s the %s way" % ("strings", "interpolated", "old") + # None is an object None # => None -- cgit v1.2.3