AppDividend
Latest Code Tutorials

Python String Format: How to Format String in Python

Python string format() is an inbuilt function that formats a given string into the more excellent output. The String.format() method is one of the string formatting methods in Python3, which allows multiple substitutions and value formatting.

The format() method lets us concatenate elements within the string through positional formatting.

Python String Format

Python String format() method takes any number of parameters. But, it is primarily divided into two types of parameters which are following.

  1. Positional parameters – It is the list of parameters that can be accessed with an index of the parameter inside curly braces {index}.
  2. Keyword parameters – It is the list of parameters of type key=value that can be accessed with a key of the parameter inside curly braces {key}.

The format() method returns the formatted string. The format() reads the type of arguments passed to it and formatted it according to the format codes defined in the string.

See the following syntax.

{}.format(value)

The value parameter can be an integer, floating-point numeric constant, string, characters or even variables.

It will return the formatted string with the value passed as a parameter in the placeholder position.

Basic formatting with a format() method

Let’s see the basic formatting in action. Write the below code inside the app.py file.

# app.py

name = 'Krunal'
coins = 21
print('Hello {}, you have {} coins.'.format(name, coins))

In the above code, The {} curly braces are replaced by name and coins variables. 

See the output.

Basic formatting with a format() method

You can also write the above code like the below code.

# app.py

name = 'Krunal'
coins = 21
print('Hello {0}, you have {1} coins.'.format(name, coins))

See the output.

Python String Format Tutorial With Example

Python String Multiple Formatters

The multiple pairs of curly braces can be used while formatting the string. Let’s say if another variable substitution is needed in a large string, it can be done by adding the second pair of curly braces and passing the second value into the method.

Python will replace the placeholders with values in order. We have already seen the example of Multiple String Formatters above.

Python Number Formatting

Let’s see the simple string number formatting in Python. Write the following code.

# app.py

inr = 1000
print('I have {:d} rupees.'.format(inr))

See the output.

Python Simple Number Formatting

Let’s take float, binary and hexadecimal datatype example.

# app.py

print('The float number is:{:f}'.format(21.19219219219))

print('bin: {0:b}, oct: {0:o}, hex: {0:x}'.format(1921))

See the output.

Python Number Formatting

You can format the numbers using a format specifier given below.

TypeMeaning
dDecimal integer.
cCorresponding Unicode character.
bBinary format.
oOctal format.
xThe hexadecimal format in lower case.
XThe hexadecimal format in the upper case.
nSame as ‘d’. Except it uses the current locale setting for number separator.
eExponential notation. (lowercase e)
EExponential notation (uppercase E)
fDisplays fixed-point number (Default: 6)
FSame as ‘f.’ Except for displays ‘inf’ as ‘INF’ and ‘nan’ as ‘NAN.’
gGeneral format. Rounds number to p significant digits. (Default precision: 6)
GSame as ‘g.’ Except for switches to ‘E’ if the number is significant.
%Percentage. Multiples by 100 and puts % at the end.

 

Finally, Python String Format Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.