Jinja - Determine the length of a string or list
by
Jeremy Canfield |
Updated: November 09 2023
| Jinja articles
length is a Jinja filter used to return the number of elements in a variable or list. For example, let's say you've created the following variables and lists.
empty string length = {{ '' | length }}
{% set emptylist=[] %}
empty list length = {{ emptylist | length }}
'Hello World' length = {{ 'Hello World' | length }}
{% set fruits=['apple','banana','grape','orange'] %}
fruits list length = {{ fruits | length }}
Something like this should be returned.
empty string length = 0
emptylist length = 0
'Hello World' length = 11
fruits list length = 4
Did you find this article helpful?
If so, consider buying me a coffee over at