1

Following from this question Stackoverflow question the answer provided has ':.' character inside the print statement. i.e

a=13.946
print("{0:.2f}".format(a))

My question is what is the ':.' called? i want to search it and learn what other options there are.

Community
  • 1
  • 1
itza
  • 58
  • 5

5 Answers5

2

They are separate things.

The .2f is a part of the format specifier which says print only the first two digits after the decimal point.

The : is another part of the format specifier as described here:

"Each field can also specify an optional set of 'format specifiers' which can be used to adjust the format of that field. Format specifiers follow the field name, with a colon (':') character separating the two:"

"My name is {0:8}".format('Fred')

Outputs 'Fred' plus 4 spaces to make 8 characters:

'My name is Fred    '
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • I have expanded on your answer with exactly what I was after in my own answer, as I work with 2.7 – itza Aug 11 '16 at 05:14
1

According to Pythong strings library 7.1.3 - Format string syntax, shows that you can add a format_spec

a format_spec, which is preceded by a colon ':' These specify a non-default format for the replacement value.

Format Specification Mini-Language shows you the whole list of options available and the context on how they can be used.

Python 2.7

Community
  • 1
  • 1
itza
  • 58
  • 5
1

:. is two separate thing. Don't get confuse and be fool by its deception and theatric

Hint ':' follow by '.'

The colon is a format spec

The dot is a leading path to mini-language , in this case 2f

Python Pro
  • 39
  • 2
0

There is no such character as :.. What you are seeing here is : followed by .2f which means a floating point number with 2 decimals.

In this code:

a=13.946
print("{0:.2f}".format(a))

The command being given in the print is convert the float into a two decimal place string. This would print 13.95

Vaibhav Bajaj
  • 1,934
  • 16
  • 29
0

Test it with some more statements, then you will understand it more:-

a = 13.946
print("{0:.2f}".format(a))

a = 13.946
print("{1:.2f}".format(a, 10))

a = 13.946
print("{2:.2f}".format(a, 10, 12))

a = 13.946
print("{3:.2f}".format(a, 10, 12))

Here is brief:-

.2f means format your number with 2 decimal places.
0 says you need to select first item passed to format.
:. is nothing but these 2 are separate thing. : is separate between item and format 
Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30
  • :. is nothing but these 2 are separate thing. : (colon) is separate between item and format – Rakesh Kumar Aug 11 '16 at 05:08
  • The colon is a 'format spec'. format spec led to string library that led to format specification mini language library. The key was format spec, which Berni's answer pointed to. That was the goal of the question. – itza Aug 11 '16 at 05:13
  • @itza colon is not a 'format spec', if you thought it a format spec then check this to execute "My name is {0} :-{{}}".format('Fred'). colon is a separate between 2 things. It's use depend on situation of use. – Rakesh Kumar Aug 11 '16 at 05:19
  • refer to [link](https://docs.python.org/2/library/string.html#formatstrings) the grammar for a replacement field is as follows `replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"` – itza Aug 11 '16 at 05:28