Details and differences of % r and % s in Python, and details of python % r % s

Source: Internet
Author: User

Details and differences of % r and % s in Python, and details of python % r % s

Explanation of % r and % s in Python

% R uses the rper () method to process objects
% S use the str () method to process objects

In some cases, the two processes the same result, for example, processing an int-type object.

Example 1:

print "I am %d years old." % 22 print "I am %s years old." % 22 print "I am %r years old." % 22 

Returned results:

I am 22 years old. I am 22 years old. I am 22 years old. 

In other cases, the two are different.

Example 2:

text = "I am %d years old." % 22 print "I said: %s." % text print "I said: %r." % text 

Returned results:

I said: I am 22 years old .. I said: 'I am 22 years old.'. // % r adds single quotes to the string

Let's look at another situation.

Example 3:

import datetime d = datetime.date.today() print "%s" % d print "%r" % d 

Returned results:

2014-04-14 datetime.date(2014, 4, 14) 

It can be seen that % r can reproduce the object it represents when printing (rper () unambiguously recreate the object it represents)

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

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.