0% found this document useful (0 votes)
20 views4 pages

Document Format

Uploaded by

9316213351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Document Format

Uploaded by

9316213351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

String Formatting using the format() method of string class

s = 'My name is {} I am {} years old and my weight is {} kg'.format(name, age, wt)

When curly braces are empty, Python will do the substitution based on order of arguments
sent in format method. So in the above example the first pair of curly braces is replaced with
name, second pair with age and third pair with wt.

Can use the index numbers inside curly braces, to decide what goes where while substituting
values inside the string.

The value 0 refers to the first argument, 1 refers to the second argument and 2 refers to the
third argument.

This way you can change the order of the variables and you can use a data value even more
than once.

In addition to these positional arguments we can send keyword arguments also.

These keyword arguments are called by their name.

You can mix both positional and keyword arguments in the same string.

Python in Depth by Deepali Srivastava


You can use conversion codes, s, d or f.
s to display the value as string
d to display the values as decimal integer (base 10)
f to display the value as a float with decimal places

When using f conversion for values, you can limit the number of digits displayed
after the decimal point. This can be done by adding a dot followed by the number of
digits after the decimal you want to be displayed.

The float value will be rounded off, if it has more decimal places than the number of decimal
places we want to display.

You can use 0 if you don’t want any decimal places to be displayed.

You can specify a width in which a given value is displayed.

By default strings are left justified in their width and numbers are right justified. To change
the justification you can use symbols < > ^
< for left justification
> for right justification
^ for centre justification

Here total 4 digits are displayed in a width of 10

Python in Depth by Deepali Srivastava


Number is displayed in a width of 10 with 4 decimal places.

By default your output fields will be padded using spaces, if you want a character to be used
for padding then you can place it just after the colon, before the alignment specifier. It is the
character that is used to display data when the data is too small to fit in the assigned field
width. It is called fill character and it can be any character except '{' or '}'. The alignment
specifier should be provided if you want to specify a padding character.

If there is a sign character, padding is done after that.

A 0 preceding width performs sign aware zero padding

You can provide a sign for numeric values.


+ Positive numbers have a + sign and negative numbers have a - sign
─ Negative numbers have a minus sign
<space> Positive numbers preceded with a space and negative numbers with a - sign.

To specify an output type you can use any of these characters.

s - For a string

Integers -
b for binary,
d for decimal base 10 notation,
x or X for hexadecimal
o for octal notation

Floating point -
e or E for exponential notation
f for fixed point notation

Python in Depth by Deepali Srivastava


You can display your numeric data with comma as the thousands separator.

For more information on % style formatting, string format method and f strings please check
the following links on python.org.

https://docs.python.org/3/library/stdtypes.html#old-string-formatting

https://docs.python.org/3/library/string.html#formatstrings

https://docs.python.org/3/reference/lexical_analysis.html#f-strings

Python in Depth by Deepali Srivastava

You might also like