Python Format() Function | String Formatting

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.

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.

Output: Hello, Eyehunt

Format Function Multiple Placeholders

How you will use format function for multiple places.

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

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.

Output: two 2 one 1
Eyehunt tutorial Python language

Type Specifying

You can include more parameters within the curly braces of formate 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

Output: Temperature 25.000000 degrees!

Temperature 25.68 degrees!

Extra Example 

Here is another way to use a format function.

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

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