AppDividend
Latest Code Tutorials

Python String Format Example | String.format() Method Tutorial

0 31

Get real time updates directly on you device, subscribe now.

Python String Format Example | String.format() Method Tutorial is today’s topic. The string format() method formats a given string into the more excellent output in Python. 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 Example

The 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.

The syntax of String Format() method is following.

{}.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. The output is following.

 

Basic formatting with a format() method

You can also write the above code like below code.

# app.py

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

The output is following.

 

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))

The output is following.

 

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))

The output is following.

 

Python Number Formatting

Related Posts
1 of 42

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

Type Meaning
d Decimal integer.
c Corresponding Unicode character.
b Binary format.
o Octal format.
x The hexadecimal format in lower case.
X The hexadecimal format in the upper case.
n Same as ‘d’. Except it uses current locale setting for number separator.
e Exponential notation. (lowercase e)
E Exponential notation (uppercase E)
f Displays fixed point number (Default: 6)
F Same as ‘f.’ Except for displays ‘inf’ as ‘INF’ and ‘nan’ as ‘NAN.’
g General format. Rounds number to p significant digits. (Default precision: 6)
G Same 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 | String.format() Method Tutorial 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.