From 0c062436a2b5bb85ecf6750f0afc1eaa01492fd4 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Mon, 27 Jul 2009 04:41:41 -0400 Subject: [PATCH] {} string formatting --- refactoring.html | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/refactoring.html b/refactoring.html index ba6fe2d..c8e521f 100755 --- a/refactoring.html +++ b/refactoring.html @@ -74,10 +74,10 @@ FAILED (failures=1)
def from_roman(s):
     '''convert Roman numeral to integer'''
-    if not s:  
+    if not s:                                                                  
         raise InvalidRomanNumeralError, 'Input can not be blank'
     if not re.search(romanNumeralPattern, s):
-        raise InvalidRomanNumeralError, 'Invalid Roman numeral: {0}'.format(s)
+        raise InvalidRomanNumeralError, 'Invalid Roman numeral: {}'.format(s)  
 
     result = 0
     index = 0
@@ -88,6 +88,7 @@ FAILED (failures=1)
return result
  1. Only two lines of code are required: an explicit check for an empty string, and a raise statement. +
  2. I don’t think I’ve mentioned this yet anywhere in this book, so let this serve as your final lesson in string formatting. Starting in Python 3.1, you can skip the numbers when using positional indexes in a format specifier. That is, instead of using the format specifier {0} to refer to the first parameter to the format() method, you can simply use {} and Python will fill in the proper positional index for you. This works for any number of arguments; the first {} is {0}, the second {} is {1}, and so forth.