Skip to content

Python Format() Function | String Formatting

  • by

Python Format Function is used to allows multiple substitutions and value formatting. This function is in build in python, which provides functionality to concatenate elements within a string through positional formatting. In Python there is one more way to do formatting stings, the “Old Style” String Formatting (% operator).

Python Format() Function | String Formatting with examples

Syntax

Using format function in python you don’t need any library to import.

Formatters work by putting in one or more placeholders (replacement fields) – defined by a pair of curly braces {} , into a string (sentence or content) and calling the str.format()function. You have to pass the value in function concatenate with the string.

"Hello, {}" .format(value)
# OR with tuple 
'one {} two {}'.format('1', '2')

Parameters : 

  • Value: Integer (Number), string, characters, or even variables.
  • Return type: Formatted string with the value passed as a parameter in the placeholder position.

Python Format Function Example 

Basic formatting example of a string. The name string value will the print into place of { } curly braces.

name = "Eyehunt"
print("Hello, {}" .format(name))

Output: Hello, Eyehunt

Format Function Multiple Placeholders

How you will use format function for multiple places.

Simple use multiple { } curly braces and add value in format fiction same as define tuple in python. Let’s see a simple example of it.

print('Python {} easy to learn in {} days'.format('Language', '30'))

Output: Python Language easy to learn in 30 days

Reordering or Positional and Keyword Arguments

How to change the order of value in placeholder?  because When we add curly braces empty without any parameters, Python will replace the values passed through the str.format() function in order. So if you want to change the order of it you have to pass the number {0} like indexing of it. Indexing starts from 0.

print('two {1}  one {0}'.format(1, 2))

print('Eyehunt {1}  Python {0}'.format('language','tutorial'))

Output: two 2 one 1
Eyehunt tutorial Python language

Type Specifying

You can include more parameters within the curly braces of format function syntax.

Type Specifying format code syntax is  {field_name:conversion}, where field_name specifies the index number of the argument and conversion refers to the conversion code of the data type that you’re using with the formatter to the str.format().

Here is a conversion code of data type :

  • s – strings
  • d – decimal integers (base-10)
  • f – floating point display
  • c – character
  • b – binary
  • o – octal
  • x – hexadecimal with lowercase letters after 9
  • X – hexadecimal with uppercase letters after 9
  • e – exponent notation
print('Temperature {0:f} degrees!'.format(25))
# Upto 2 decimal point
print('Temperature {0:0.2f} degrees!'.format(25.6784568))

Output: Temperature 25.000000 degrees!

Temperature 25.68 degrees!

Extra Example 

Here is another way to use a format function.

str = "Eyehunt {}"

print(str.format('tutorial'))

Output: Eyehunt tutorial

Note : This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Python Format Function Examples are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *