Skip to main content

Strings

Strings are sequences of unicode characters. Their methods and filters help you work with text. String-specific operators, tests, filters, and methods are described below. Since strings are also sequences, you can apply any sequence operators, tests, and filters to strings. We duplicate the most important of them on this page.

Literals

"Hello World"

String literals are surrounded by either single quotation marks, or double quotation marks.

{{ "hello" }}
-> hello

{{ 'hello' }}
-> hello

See also: Jinja Docs.

Constructors

any|string

Convert an object to a string if it isn’t already.

{{ true|string|upper }}
-> TRUE

See also: Jinja Docs.

Operators

string[start:stop]

Gets a substring with the character at start and the last character set to index before stop. Indexes start and stop can be omitted which means that the result includes characters from the beginning or end of the string.

{% set input = "This is a text." %}

{{ input[5:15] }}
-> "is a text."

{{ input[5:] }}
-> "is a text."

{{ input[:15] }}
-> "This is a text."

See also: Python Docs, In-depth Tutorial on DigitalOcean.

string % x

Given string % x, instances of % in string are replaced with zero or more elements of values. This operation is commonly known as string interpolation.

{% set greeting = "Hello" %}
{% set name = "John" %}

{{ "%s, %s!" % (greeting, name) }}
-> Hello, John!

In most cases it should be more convenient and efficient to use the str.format():

{{ "{}, {}!".format(greeting, name) }}
-> Hello, John!

See also: Python Docs, printf-style String Formatting, str|format, str.format().

x in string

Return true if the right string contains the left substring.

{% set input = "I said yes!" %}

{{ "yes" in input }}
-> true # because the input string contains substring "yes"

See also: Jinja Docs.

x not in string

Return true if the right string does not contain the left substring.

{% set input = "I said no!" %}

{{ "yes" in input }}
-> false # because the input string does not contain substring "yes"

See also: Jinja Docs.

string1 ~ string2

Converts all operands into strings and concatenates them.

{% set name = "John" %}

{{ "Hello " ~ name ~ "!" }}
-> Hello John!

See also: Jinja Docs.

string * number

Repeat a string multiple times.

{{ "=" * 8 }}
-> ========

See also: Jinja Docs.

Tests

x is in string

Check if x is in string. Typicallty you need to use operator x in string instead of a test. Use a test when you need filter a list of possible substrings.

{% set input = "I said yes!" %}

{{ "yes" is in input }}
-> true

{{ ["yes", "yep", "ok"]|select("in", input)|list }}
-> ["yes"]

See also: Jinja Docs, x in string, list|select, iterable|list.

x is lower

Return true if x is lowercased.

{{ "onion" is lower }}
-> true

{{ ["onion", "olives", "HAM"]|select("lower")|list }}
-> ["onion", "olives"]

See also: Jinja Docs, list|select, iterable|list, Methods: Character Case.

x is string

Return true if x is a string.

{{ "hello world" is string }}
-> true

{{ [true, "hello world", 12]|select("string")|list }}
-> ["hello world"]

See also: Jinja Docs, list|select, iterable|list.

x is upper

Return true if x is uppercased.

{{ "HAM" is upper }}
-> true

{{ ["onion", "olives", "HAM"]|select("upper")|list }}
-> ["HAM"]

See also: Jinja Docs, list|select, iterable|list, Methods: Character Case.

Filters

str|capitalize

Capitalize a value. The first character will be uppercase, all others lowercase.

{% set input = "hello world!" %}

{{ input|capitalize }}
-> Hello world!

See also: Jinja Docs, Methods: Character Case.

str|format

Apply the given values to a printf-style format string, like string % values.

{{ "%s, %s!"|format(greeting, name) }}
Hello, World!

In most cases it should be more convenient and efficient to use the str.format():

{{ "{}, {}!".format(greeting, name) }}

See also: Jinja Docs, string % x, Methods: Formatting.

str|indent

Return a copy of the string with each line indented by 4 spaces. The first line and blank lines are not indented by default.

Parameters

  • width – Number of spaces, or a string, to indent by.
  • first – Don’t skip indenting the first line.
  • blank – Don’t skip indenting empty lines.

See also: Jinja Docs, Methods: Formatting.

str|length

Return the number of characters in a string.

{% set input = "Hello" %}

{{ input|length }}
-> 5

See also: Jinja Docs.

str|lower

Convert a string to a lowercase.

{% set input = "This is A DOG!" %}

{{ input|lower }}
-> this is a dog!

See also: Jinja Docs, Methods: Character Case.

str|replace

Return a copy of the value with all occurrences of a substring replaced with a new one.

Parameters

  • old* - The substring that should be replaced.
  • new*- The replacement string.
  • count - If givven, only the first count occurrences are replaced.

Examples

{{ "Hello World"|replace("Hello", "Goodbye") }}
-> Goodbye World

{{ "AAA"|replace("A", "B", 2) }}
-> BBA

See also: Jinja Docs, Methods: Replacing.

str|title

Return a titlecased version of the value. I.e. words will start with uppercase letters, all remaining characters are lowercase.

See also: Jinja Docs, Methods: Character Case.

str|trim

Strip leading and trailing characters, by default whitespace.

Parameters

  • chars - A string containing characters to strip.

Examples

{% set input = "   something is here    " %}

Result: "{{ input|trim }}".
-> Result: "something is here".

See also: Jinja Docs, Methods: Stripping.

str|truncate

Return a truncated copy of the string.

Strings that only exceed the length by the tolerance margin given in the fourth parameter will not be truncated.

Parameters

  • length - The maximum length of the string to keep on (default=255).
  • killwords - If true, the cut the text at length, otherwise discard the last word (default=false).
  • end - A string to append if the text is in fact truncated (default="...").
  • leeway - Strings that only exceed the length by the tolerance margin given will not be truncated (default=5).

Examples

{{ "foo bar baz qux"|truncate(9) }}
-> "foo..."

{{ "foo bar baz qux"|truncate(9, true) }}
-> "foo ba..."

{{ "foo bar baz qux"|truncate(11) }}
-> "foo bar baz qux"

{{ "foo bar baz qux"|truncate(11, false, '...', 0) }}
-> "foo bar..."

See also: Jinja Docs, Methods: Formatting.

str|upper

Convert a string to uppercase.

{% set input = "hi there" %}

{{ input|upper }}
-> HI THERE

See also: Jinja Docs, Methods: Character Case.

str|wordcount

Count the words in that string.

{% set input = "Hello, world!" %}

{{ input|wordcount }}
-> 2

See also: Jinja Docs.

Methods

Methods: Replace

str.replace(old, new)

Return a copy of the string with all occurrences of substring old replaced by new. See Python Docs.

Methods: Prefixes, Suffixes

str.startswith(prefix)

Return true if string starts with the prefix, otherwise return false.

{% set input = "What is your name?" %}

{{ input.startswith("What") }}
-> true

See also: Python Docs.

str.endswith(suffix, ...)

Return true if the string ends with the specified suffix, otherwise return false.

{% set input = "What is your name?" %}

{{ input.endswith("?") }}
-> true

See also: Python Docs.

str.removeprefix(prefix)

Return a copy of the string without the prefix. See Python Docs.

str.removesuffix(suffix)

Return a copy of the string without the suffix. See Python Docs.

Methods: Strip

str.strip([chars], ...)

Return a copy of the string with the leading and trailing characters removed. See Python Docs.

str.lstrip([chars])

Return a copy of the string with leading characters removed. See Python Docs.

str.rstrip([chars])

Return a copy of the string with trailing characters removed. See Python Docs.

Methods: Split

str.partition(sep)

Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. See Python Docs.

str.rpartition(sep)

Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. See Python Docs.

str.rsplit(sep)

Return a list of the words in the string, using sep as the delimiter string. See Python Docs.

str.split(sep, ...)

Return a list of the words in the string, using sep as the delimiter string. See Python Docs.

str.splitlines(...)

Return a list of the lines in the string, breaking at line boundaries. See Python Docs.

Methods: Format

str.format(...)

Perform a string formatting operation. See Python Docs.

{{ "The sum of 1 + 2 is {0}".format(1+2) }}
-> The sum of 1 + 2 is 3

See also: Format String Syntax, str|format, string % x.

str.join(iterable)

Return a string which is the concatenation of the strings in iterable. See Python Docs.

See also: list|join.

str.center(width, ...)

Return centered in a string of length width. See Python Docs.

str.expandtabs(...)

Return a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. See Python Docs.

str.ljust(width)

Return the string left justified in a string of length width. See Python Docs.

str.rjust(width)

Return the string right justified in a string of length width. See Python Docs.

str.zfill(width)

Return a copy of the string left filled with ASCII '0' digits to make a string of length width. See Python Docs.

Methods: Character Case

str.capitalize()

Return a copy of the string with its first character capitalized and the rest lowercased. See Python Docs.

str.casefold()

Return a casefolded copy of the string. Casefolded strings may be used for caseless matching. See Python Docs.

str.upper()

Return a copy of the string with all the cased characters 4 converted to uppercase. See Python Docs.

str.lower()

Return a copy of the string with all the cased characters converted to lowercase. See Python Docs.

str.title()

Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase. See Python Docs.

str.swapcase()

Return a copy of the string with uppercase characters converted to lowercase and vice versa. See Python Docs.

Methods: Predicates

str.isalnum()

Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. See Python Docs.

str.isalpha()

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. See Python Docs.

str.isascii()

Return true if the string is empty or all characters in the string are ASCII, false otherwise. See Python Docs.

str.isdecimal()

Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. See Python Docs.

str.isdigit()

Return true if all characters in the string are digits and there is at least one character, false otherwise. See Python Docs.

str.islower()

Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise. See Python Docs.

str.isnumeric()

Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. See Python Docs.

str.isprintable()

Return true if all characters in the string are printable or the string is empty, false otherwise. See Python Docs.

str.isspace()

Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. See Python Docs.

str.istitle()

Return true if the string is a titlecased string and there is at least one character. Return false otherwise. See Python Docs.

str.isupper()

Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise. See Python Docs.

Methods: Substrings

str.find(sub, ...)

Return the lowest index in the string where substring sub is found. See Python Docs.

str.index(sub, ...)

Like str.find(), but raise an errors when the substring is not found. See Python Docs.

str.rfind(sub, ...)

Return the highest index in the string where substring sub is found. See Python Docs.

str.rindex(sub, ...)

Like str.rfind(), but raise an errors when the substring is not found. See Python Docs.

str.count(sub, ...)

Return the number of non-overlapping occurrences of substring sub. See Python Docs.