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 โ
python
aString1 = "Hello World"
aString2 = sp.string("Hello World")
Operations โ
Concatenation โ
expr1 + expr2
Concatenate two sequences of sp.TString.
Example โ
python
"Hello" + " " + "World" # Hello World
sp.concat(<l>)
Concatenate a list l
of sp.TString.
Example โ
python
Michelson CONCATsp.concat(["Hello", " ", "World"]) # Hello World
Obtaining Size โ
sp.len(<expression>)
Return the length of <expression>
.
Example โ
python
Michelson SIZEsize = sp.len("A String")
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 โ
python
Michelson SLICEsp.slice("Hello World", 0, 5) # sp.some("Hello")