%R using the Rper () method to process objects%s processing an object with the Str () method In some cases, the result of both processing is the same, for example, to deal with an int type object. Example one:Print "I am%dyears old. " % A Print "I am%syears old. " % A Print "I am%ryears old. " % AI am AYears old. I am AYears old. I am AYears old. In some cases, the two are different. Example two: text= "I am%dyears old. " % A Print "I said:%s." %TextPrint "I said:%r." %Text return result: I said:i am AYears old. I said:' I am years old. '.// %R adds a single quotation mark to the string and then one case three:>> fromDatetimeImportDatetime>>Timeinfo=Datetime.today ()>>Timeinfodatetime.datetime ( .,6,7, +, -, the,925488)>> type(Timeinfo) Datetime.datetime>> repr(Timeinfo)' Datetime.datetime (6, 7, +, +, 925488) '>> Str(Timeinfo)' 2016-06-07 21:17:34.925488 '%R prints the parameters given back as they are, with type information. Formatter= "%r %r %r %r" PrintFormatter%(1,2,3,4)PrintFormatter%("One","both","three","Four")PrintFormatter%(True,False,False,True)PrintFormatter%(Formatter, formatter, formatter, formatter)PrintFormatter%("I had this thing.","That's could type up right.","But it didn ' t sing.","So I said Goodnight.") Output: $ python ex8.py1 2 3 4' One ' ' both ' ' three ' ' Four 'True False False True'%r%r%r%r ' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'' I had this thing. ' ' That's could type up right. ' "But it didn ' t sing." ' so I said Goodnight. '$
The difference between%r and%s in Python