Programming

5 Useful String Format() Function in Python

To control and handle complex string formatting more efficiently

Amit Chauhan
Towards AI
Published in
7 min readJun 20, 2021

--

Photo by Jefferson Santos on Unsplash

What is formatting, why is it used?

In python, there are several ways to present output. String formatting using python is one such method where it allows the user to control and handle complex string formatting more efficiently than simply printing space-separated values.

There are many types of string formatting, such as padding and alignment, using dictionaries, etc. The usage of formatting techniques is not only subjected to strings. It also formats dates, numbers, signed digits, etc.

Structure of format() method

Let us look at the basic structure of how to write in string format method.

Syntax: ‘String {} value’.format(value)Let us look at an example:
‘Welcome to the {} world.’.format(“python”)

Here, we have defined a string( ‘’) with a placeholder( {} ) and assigned the argument of the parameter as “python.” On executing the program, the value will be assigned to the placeholder, showing the output as:

Formatting Methods

--

--