Python format() function



In Python, the format() function is a powerful tool that simplifies string formatting. It allows you to create formatted strings by replacing placeholders with specific values. This article will guide you through the various features and techniques of using the format() function to simplify string formatting in Python.

Example of format() Method

The format() method takes one or more arguments and returns a formatted string. Let’s look at an example:

name = "Alice"
age = 25
message = "My name is {} and I am {} years old.".format(name, age)
print(message)

The output will be:

My name is Alice and I am 25 years old.

In the above example, we have used curly braces ({}) as placeholders in the string. The values passed to the format() method are substituted into the respective placeholders, resulting in the formatted string.

Using Multiple Formatters

You can use multiple formatters within a string to format multiple values. For instance:

name = "Bob"
age = 30
height = 180
message = "My name is {}, I am {} years old, and my height is {} cm.".format(name, age, height)
print(message)

The output will be:

My name is Bob, I am 30 years old, and my height is 180 cm.

Formatting Strings using Escape Sequences

The format() method allows you to use escape sequences to format strings. For example:

name = "John"
message = "Hello, {}!\nHow are you today?".format(name)
print(message)

The output will be:

Hello, John!
How are you today?

Formatters with Positional and Keyword Arguments

You can also use positional and keyword arguments with the format() method. Consider the following example:

name = "Emma"
age = 28
message = "My name is {0} and I am {age} years old.".format(name, age=age)
print(message)

The output will be:

My name is Emma and I am 28 years old.

Type Specifying

You can specify the data type of the value being formatted using type specifiers. Here’s an example:

number = 42
message = "The answer to life, the universe, and everything is {:d}.".format(number)
print(message)

The output will be:

The answer to life, the universe, and everything is 42.

Padding Substitutions or Generating Spaces

You can use padding to generate spaces before or after the substituted value. Here’s an example:

name = "Kate"
message = "Hello, {:>10}!".format(name)
print(message)

The output will be:

Hello,       Kate!

In the above example, we have specified a field width of 10 characters and right-aligned the value within that field.

Applications

The format() function is incredibly versatile and has a wide range of applications. Here are a few examples:

  • Generating formatted output in reports or logs.
  • Creating dynamic SQL queries.
  • Displaying formatted data in user interfaces.

Using a Dictionary for String Formatting

You can use a dictionary to provide key-value pairs for string formatting. Here’s an example:

person = {"name": "Sam", "age": 35}
message = "My name is {name} and I am {age} years old.".format(**person)
print(message)

The output will be:

My name is Sam and I am 35 years old.

In the above example, we have used a dictionary to supply the values for the placeholders in the string.

Last Updated on May 18, 2023 by admin

Leave a Reply

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

Recommended Blogs