On which parameter does python distinguish between a formatted string and a normal string?

x = f"There are {n} types of people"

print(type(x)==type("HELLO")) #returns True

If the formatted string and the normal string are of the same type. How does a function differentiate when it is formatted or when not?

My assumption is that whenever I point fin front of a string, the interpreter selects the value of the variables and formats it then and there, and the function receives a formatted string.

Is this a shorthand notation like lambdas in Java 8?

+4
source share
3 answers

In your example:

x = f"There are {n} types of people"

xis never an f-string, it's just a regular string that has already replaced {n}with a variable value n.

f- str.

+2

PEP 498:

F- , . , f- - , , . Python f-string - "f", . .

( )

+1

They are of the same type.

n = 5
f"There are {n} types of people"

is just a new convenient way to insert variables into a string introduced in Python 3.6

It can also be written as

n = 5
"There are {:d} types of people".format(n)
0
source

Source: https://habr.com/ru/post/1693587/


All Articles