format-function-features
---
id: format-function-features
title: Displaying Integers with the format() Function
description: How to display integer and floating-point data in various formats using the format() function
tags:
- format function
- string formatting
- Python
- programming
sidebar_position: 3
isPublic: false
---
# Displaying Integers with the format() Function
Using the `format()` function, you can output strings in various formats.
This process is known as `formatting`, and it involves using a colon `:` inside the curly braces `{ }` to specify how the data should be displayed.
<br />
```python title="Example of using the format() function"
"{:format_option}".format(value)
-
{ }
: Placeholder indicating where to insert the value -
:
: Specifies format options
Displaying Integers
To output integers, use {:d}
by placing the letter d
to the right of the colon inside the curly braces.
Integer output formatting example
number = 123
# Integer output
formatted = "number: {:d}".format(number)
print(formatted) # "number: 123"
If you omit d
, Python will automatically apply the appropriate format based on the value's data type.
Automatic type specification formatting example
number = 123
# Integer output
formatted = "number: {}".format(number)
print(formatted) # "number: 123"
Specifying Output Width
Using a number to the right of the colon specifies the width of the resulting string.
For instance, {:5}
sets the width of the output string to 5
.
Integer output formatting example
number = 123
formatted = "number: {:5}".format(number) # Set width to 5
# Inserts 2 spaces before 123
print(formatted) # "number: 123",
To pad the width with zeros, prefix the width value with a 0
.
Integer output formatting example
number = 123
formatted = "number: {:05}".format(number) # Set width to 5
# Inserts 2 zeros before 123
print(formatted) # "number: 00123"
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.