Python String Format() Method With Examples

The string format() class in the string module allows programmers to create and customize their own string formatting behaviors using the built-in methods. The String format() is an in-built public method of Formatter class to perform a string formatting operation. The format() is the primary API method. It takes a format string and an arbitrary set of positional and keyword arguments. The string format() has n number of arguments. These arguments are divided into two types. One is positional arguments and another one is keyword arguments.

Syntax:

 String_template.format( p0, p1,…….k0 = v0, k1 = v1,…….)

In the above syntax, p0, p1 are the positional arguments and, k0, k1 are the keyword arguments with the respective values v0, v1.

The parameters of String format():

Positional Arguments: It is a list of arguments that can be used with arithmetic progression or index number using curly braces.

Keyword Arguments: It is a list of arguments that can be used to assign value to the keys using curly braces.

The return value from String format():

The string format() customizes the string values and returns the formatted string.

Using a Single Formatter:

Here more than one replacement field and placeholders are used by a single formatter with a pair of curly braces. The argument passes through the format() and concatenates with the string.

Syntax: {}. format (value)

 Sample code:

name = "Peter"
message = "Hello {}, Welcome to Techieclues."
print(message.format(name))

# Output
# Hello Peter, Welcome to Techieclues.

Where, the value can be an integer, characters, string, floating-point numbers, or any variables also. This format returns a formatted string passing the value in the placeholder.

Using Multiple Formatters:

In this formatter, multiple curly braces are used to format string. In Python, the placeholders are replaced by respective values in order.

Syntax: {}{}. Format (v1, v2)

Sample code:

name = "Peter"
designation = "Software Engineer"
message = "My name is {}. I'm working as a {}"
print(message.format(name, designation))

# Output
# My name is Peter. I'm working as a Software Engineer.

Here v1 and v2 can be integers, characters, string, floating-point numbers, or any variables also. The number of values is passed through the format() must be equal to the number of placeholders in that string format. Otherwise, it generates error due to the wrong use of placeholders and passing extra or less value as an argument. This format returns a formatted string passing the value in the placeholder.

Formatters with Positional and Keyword Arguments:

This formatter is created using positional arguments and keyword arguments. The values that are passed through the format() as arguments are usually tuple data types. Individual tuples are called by respective index numbers. By default, the starting index number is 0

Syntax: {0}{1}. format (p, k)

Sample code:

name = "Peter"
designation = "Software Engineer"
exp = "10"
message = "My name is {0}. I'm working as a {1} and my experience is {2} years"
print(message.format(name, designation, exp))

# Output
# My name is Peter. I'm working as a Software Engineer and my experience is 10 years.

Here, p represents the positional argument and k represents the keyword argument. The positional arguments can be integers, characters, string, floating-point numbers, or any variables also. The keyword arguments are some values that are passed through format() as an argument.

Type Specifying:

A list of conversation code is given.

Code Description
s It represents strings
d It represents decimal integers (base-10)
f It represents a floating-point number
c It represents a character
b It represents binary
o It represents octal
x It represents a hexadecimal number with lowercase letters after 9
X It represents a hexadecimal number with uppercase letters after 9
e It represents exponent notation

Syntax : String {field_name:conversion}

Padding Substitutions or Generating Spaces:

In Python, strings are by default left-aligned. The arithmetic integers are right-aligned.

Sign Description
< It represents left-align text in the field
^ It represents center text in the field
> It represents right-align text in the field