Skip to content
On this page

Strings โ€‹

The type of SmartPy strings is sp.TString.

The corresponding type in Michelson is

Michelson string.

See reference Strings and Bytes template.

Literals โ€‹

"...", '...' or sp.string(s)
Strings in SmartPy are introduced by simply using regular Python strings of the form "..." or '...', or by using sp.string(s) where s is a Python string.

Example โ€‹

aString1 = "Hello World"
aString2 = sp.string("Hello World")

Operations โ€‹

Concatenation โ€‹

expr1 + expr2
Concatenate two sequences of sp.TString.

Example โ€‹

"Hello" + " " + "World" # Hello World

Concatenate a list l of sp.TString.

Example โ€‹

sp.concat(["Hello", " ", "World"]) # Hello World
Michelson CONCAT

Obtaining Size โ€‹

Return the length of <expression>.

Example โ€‹

size = sp.len("A String")
Michelson SIZE

Slicing โ€‹

sp.slice(<expression>,ย offset=<offset>,ย length=<length>)
Slice expression of type sp.TString from offset for length characters.

It returns an expression of type sp.TOption(sp.TString).

Example โ€‹

sp.slice("Hello World", 0, 5) # sp.some("Hello")
Michelson SLICE