Skip to main content
Log in

Mojo module

format

String formatting utilities for Mojo.

This module provides string formatting functionality similar to Python's str.format() method. The format() method (available on the String and StringSlice types) takes the current string as a template (or "format string"), which can contain literal text and/or replacement fields delimited by curly braces ({}). The replacement fields are replaced with the values of the arguments.

Replacement fields can mapped to the arguments in one of two ways:

  • Automatic indexing by argument position:

    var s = String("{} is {}").format("Mojo", "🔥")
    var s = String("{} is {}").format("Mojo", "🔥")
  • Manual indexing by argument position:

    var s = String("{1} is {0}").format("hot", "🔥")
    var s = String("{1} is {0}").format("hot", "🔥")

The replacement fields can also contain the !r or !s conversion flags, to indicate whether the argument should be formatted using repr() or String(), respectively:

var s = String("{!r}").format(myComplicatedObject)
var s = String("{!r}").format(myComplicatedObject)

Note that the following features from Python's str.format() are not yet supported:

  • Named arguments (for example "{name} is {adjective}").
  • Accessing the attributes of an argument value (for example, "{0.name}".
  • Accessing an indexed value from the argument (for example, "{1[0]}").
  • Format specifiers for controlling output format (width, precision, and so on).

Example:

from collections.string import String

# Basic formatting
var s1 = String("Hello {0}!").format("World") # Hello World!

# Multiple arguments
var s2 = String("{0} plus {1} equals {2}").format(1, 2, 3) # 1 plus 2 equals 3

# Conversion flags
var s4 = String("{!r}").format("test") # "'test'"
from collections.string import String

# Basic formatting
var s1 = String("Hello {0}!").format("World") # Hello World!

# Multiple arguments
var s2 = String("{0} plus {1} equals {2}").format(1, 2, 3) # 1 plus 2 equals 3

# Conversion flags
var s4 = String("{!r}").format("test") # "'test'"

This module has no public API; its functionality is available through the String.format() and StringSlice.format() methods.