---
title: '|stringformat'
description: formats a string.
date: '2026-08-02'
categories:
  - Filter
  - String
canonical: https://www.djangotemplatetagsandfilters.com/filters/stringformat/
doc_link: https://docs.djangoproject.com/en/6.0/ref/templates/builtins/#stringformat
---

# |stringformat

formats a string.

## Documentation

Formats the value using the [old printf-style string formatting.](https://docs.python.org/3/library/stdtypes.html#old-string-formatting)

### Variable

```django
pi = math.pi
```

### Template

```django
{{ pi|stringformat:'E' }}
```

### Result

```django
3.141593E+00
```

## Commentary

For some types of websites, this might be super useful, but we find our strings are usually stored in the format in which we want to output them. And for numbers [`floatformat`](/filters/floatformat/) is generally more useful.

One case where `stringformat` can be useful: comparing a value on the querystring, which is always a string, with an integer property of an object (e.g., an id). For example:

```django
<select name="user" id="user" class="form-select">
  <option value="">User</option>
  {% for user in users %}
    <option value="{{ user.id }}"{% if request.GET.user == user.id|stringformat:"i" %} selected{% endif %}>{{ user.full_name }}</option>
  {% endfor %}
</select>
```

Thanks to Simeon Visser for [this Stack Overflow answer](https://stackoverflow.com/a/27771117/5128561).
