Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python shorthand for .format [duplicate]

Tags:

python

Ahead Warning! A Python newbie and quite a spoiled question ahead!

Is there a way to shorthand:

"{} blabla ... {}".format(x, y)

Into something like:

"{} blabla ... {}" % (x, y)

Using operator overloading like syntax or otherwise?

Not talking about old style string formatting which took typed %s ...

like image 362
rubmz Avatar asked Oct 19 '17 14:10

rubmz


People also ask

What does .2f mean in Python?

A format of . 2f (note the f ) means to display the number with two digits after the decimal point. So the number 1 would display as 1.00 and the number 1.5555 would display as 1.56 .

What is %s and %D Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values. For example (for python 3) print ('%s is %d years old' % ('Joe', 42)) Would output Joe is 42 years old.

How do you write .2f in Python?

As expected, the floating point number (1.9876) was rounded up to two decimal places – 1.99. So %. 2f means to round up to two decimal places. You can play around with the code to see what happens as you change the number in the formatter.

Why %F is used in Python?

Python f-string is the newest Python syntax to do string formatting. It is available since Python 3.6. Python f-strings provide a faster, more readable, more concise, and less error prone way of formatting strings in Python.


1 Answers

Use this in Python 3.6 or above:

f"{x} blabla ... {y}"
like image 101
Dabiuteef Avatar answered Sep 28 '22 22:09

Dabiuteef