- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When to use %r instead of %s in Python?
The %s specifier converts the object using str(), and %r converts it using repr().
For some objects such as integers, they yield the same result, but repr() is special in that (for types where this is possible) it conventionally returns a result that is valid Python syntax, which could be used to unambiguously recreate the object it represents. For example, if you have a string with endline characters, %s would actually show stuff on new lines while %r would just give the output as \n and also keep the quotes intact.
For example
>>> string = "Hello\nworld" >>> print "Example: %s" % string Example: Hello world >>> print "Example: %r" % string Example: 'Hello\nworld'
You can use the second expression to actually recreate the object.
- Related Articles
- When to use yield instead of return in Python?
- How to use column index instead of column name while using group_by of dplyr in R?
- When to call the Thread.run() instead of Thread.start() in Java?
- Why you should use NumPy arrays instead of nested Python lists?
- How to perform rounding in R to next 10 instead of nearest 10?
- Why use static_cast(x) instead of (int)x in C++?
- How to use COUNT(*) to return a single row instead of multiple?
- How to create a histogram with dots instead of bars in base R?
- Why do we use WebDriver instead of Selenium IDE?
- How to force MongoDB to use the BasicCursor instead of an index?\n
- Which fossil fuel is conserved:(a) when we save on electricity?(b) when we use bicycle for covering short distances instead of a motorbike?
- Why should we use a StringBuffer instead of a String in Java?\n
- Do you think it is better to use compost instead of chemical fertilisers? Why?
- When to use inline function and when not to use it in C/C++?
- When to use an abstract class and when to use an interface in Java?

Advertisements