Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python format (f-string) strings, what does !r mean? [duplicate]

I get what the new f strings in python 3.6 do, but what about the ending !r as found in the code snip below.

def __repr__(self):     return (f'Pizza({self.radius!r}, 'f'{self.ingredients!r})') 
like image 916
nk abram Avatar asked Jun 28 '17 11:06

nk abram


People also ask

What is R in Python F-string?

f-strings include a directive that allows the repr of a Python object to be included in the final output. The repr of a Python object is a debugging description of the given Python object.

What does R in text mean in Python?

In Python, when you prefix a string with the letter r or R such as r'...' and R'...' , that string becomes a raw string. Unlike a regular string, a raw string treats the backslashes ( \ ) as literal characters.

What does R in front of a string mean?

The r prefix on strings stands for “raw strings”. Standard strings use backslash for escape characters: “\n” is a newline, not backslash-n.

What does exclamation mark R mean in Python?

It just calls the repr of the value supplied. It's usage is generally not really needed with f-strings since with them you can just do repr(self.


1 Answers

It just calls the repr of the value supplied.

It's usage is generally not really needed with f-strings since with them you can just do repr(self.radius) which is arguably more clear in its intent.

!r (repr), !s (str) and !a (ascii) were kept around just to ease compatibility with the str.format alternative, you don't need to use them with f-strings.

like image 78
Dimitris Fasarakis Hilliard Avatar answered Sep 30 '22 19:09

Dimitris Fasarakis Hilliard