Python format() Function – Tutorial with Examples

The format() function in Python is a built-in function that is used to format a string. It provides a way to insert values into a string in a specified format. The format() function can be used with or without positional or keyword arguments.

Syntax

format(value[, format_spec])

or

'{}'.format(value)

Parameters

  • value – The value to be inserted into the string
  • format_spec – An optional string that specifies the format of the inserted value

Return Value

The format() function returns a string with the specified value inserted into it, formatted according to the format specification if one is provided.

Examples

Example 1: Inserting a Value into a String

name = "John"
sentence = "My name is {}.".format(name)
print(sentence)

Output:

My name is John.

In this example, a string name is created and stored. The format() function is then used to insert this value into a sentence string. The {} placeholder is used to specify where the value should be inserted in the string. The format() function then replaces this placeholder with the value stored in the name variable, and the resulting string is printed.

Example 2: Formatting a Floating-Point Number

pi = 3.14159265
sentence = "The value of pi is {:.2f}.".format(pi)
print(sentence)

Output:

The value of pi is 3.14.

In this example, the value of pi is stored as a floating-point number. The format() function is then used to insert this value into a sentence string. The {:.2f} format specification is used to specify that the inserted value should be formatted as a floating-point number with 2 decimal places. The format() function then replaces the {:.2f} placeholder with the value stored in the pi variable, formatted according to the specification, and the resulting string is printed.

Example 3: Formatting an Integer

age = 30
sentence = "I am {} years old.".format(age)
print(sentence)

Output:

I am 30 years old.

In this example, an integer value age is stored. The format() function is then used to insert this value into a sentence string. The {} placeholder is used to specify where the value should be inserted in the string. The format() function then replaces this placeholder with the value stored in the age variable, and the resulting string is printed.

Use Cases

The format() function can be used in a variety of scenarios to insert values into strings, such as:

  • Formatting string output for printing or display
  • Building strings to be used in file paths, URLs, or other situations where data must be inserted into a string
  • Constructing messages or sentences that require dynamic content
  • Inserting values into HTML or XML templates
  • Generating strings for logging or debugging purposes

The versatility of the format() function and its ability to format values according to specifications make it a powerful tool for string manipulation in Python.

Leave a Reply

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