The difference between%r and%s in Python

Source: Internet
Author: User

%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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.