return "{0!r}" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

return "{0!r}"

Hello all. Although I haven't really had much luck with getting questions answered I figured I'd try again. I recently came across the above code in a book I'm reading and am not too sure what it does... Anyone?

9th Oct 2019, 10:03 AM
Chad Williams
Chad Williams - avatar
1 Answer
+ 2
"{0!r}" is a formatting string which must be used in conjunction with format(obj), where '0' means the first argo of format(), and '!r' is a modifier flag, which instruct to pass obj to __repr__() function. In case of strings, used in place of obj, __repr__() is built-in and will render the string surrounded by single quotes. So, for example, print("{0!r}".format("sololearn")) will simply output 'sololearn'
9th Oct 2019, 11:29 AM
Bilbo Baggins
Bilbo Baggins - avatar