Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - String Format Method

Python

python logo

In this tutorial we will learn about the string format method in Python.

In Python we get the format method which allows us to format string by substituting multiple values at different positions in the string.

Single value substitution

This works by putting {} in a string and then calling the format() method.

Syntax

{}.format(value)

Where {} is a placeholder which is substituted by the value.

The value can be a character, string, integer, floating point number and even a variable.

Example

In the following example we are formatting the given string.

# string format
str = "Hello {}".format("World")

# output
print(str)            # Hello World

In the above code the value World will be insert at {} position in the string "Hello {}".

Example: Substituting variable

In the following example we are substituting the value of a variable in the given string.

# string
s = "World"

# string format
str = "Hello {}".format(s)

# output
print(str)            # Hello World

Multiple value substitution

We can place multiple {} in a string and then substitute more that one values using the format method.

Syntax

{}{}{}.format(value1, value2, value3)

In the above syntax we are substituting 3 values at three positions.

Example

In the following example we are substituting 3 values in the given string.

# string formatting
str = "The quick {} fox {} over {} lazy dog.".format("brown", "jumps", "the")

# output
print(str)

We will get the following output for the above code.

The quick brown fox jumps over the lazy dog.

Positional substitution

We can set position numbers like {i} and it will be substituted by the value at ith position in the format method.

Syntax

{0}{1}{2}.format(value0, value1, value2)

In the above syntax value0 will be inserted at the placeholder {0}.

value1 will be inserted at the placeholder {1} and value2 will be inserted at the placeholder {2}.

Index number starts from 0.

Example

In the following example we are substituting values at two positions.

# string formatting
str = "The quick brown {0} jumps over the lazy {1}.".format("fox", "dog")

# output
print(str)

In the above Python program the value fox is inserted at position {0} and the value dog is inserted at the position {1}.

Another example

In the following example we are substituting values at three positions.

# string formatting
str = "The quick {2} {0} jumps over the lazy {1}.".format("fox", "dog", "brown")

# output
print(str)     # The quick brown fox jumps over the lazy dog.

The insertion occurs in the following order.

{0} --> fox
{1} --> dog
{2} --> brown

Field specific substitution

The value is substituted at the position after going through the conversion.

{position:conversion}.format(value)

And following are some of the conversion type.

ConversionDescription
sString
dIntegers (base 10)
fFloating point numbers
bBinary
oOctal
xHexadecimal with lower case letters
XHexadecimal with upper case letters
eExponent notation

Example

In the following Python program were are substituting the values using format method.

print("Hello {0:s}!!!".format("World"))             # Hello World!!!

print("This is an integer {0:d}".format(10))        # This is an integer 10

print("Float value {0:f}".format(123.4567))         # Float value 123.456700

print("Float value {0:.2f}".format(123.4567))       # Float value 123.46

print("Binary value {0:b}".format(12))              # Binary value 1100

print("Octal value {0:o}".format(12))               # Octal value 14

print("Hex lower case {0:x}".format(29))            # Hex lower case 1d

print("Hex upper case {0:X}".format(29))            # Hex upper case 1D

The {0:.2f} inserts a floating point value with 2 decimal place.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT