Store text written in quotes or converted with str(value). Strings cannot be changed in place; string methods return new values. Positions and length count Unicode code points: an emoji is one position, but a letter with a separate accent or a joined emoji can use several.
Syntax
String
This class cannot be constructed directly.
Returns
—
Example
name = "Start"
if name:
first_char = name[0]
text(first_char)
Methods
24 methods
String.literal
Creation Method
Create text directly with single or double quotes.
Convert a value to text. Large collections or deeply nested values are shortened with markers showing omitted content; a string passed directly is unchanged.
String — One-character string. Raises IndexError when the position is outside the string.
Example
label = "Play"
first = label[0] # returns 'P'
last = label[-1] # returns 'y'
String.slice
Method
Return a new string from start up to, but not including, end. Negative indexes count from the end. Omitted bounds select the whole string in the step direction.
Syntax
string[start:end[:step]]
Parameters
Name
Description
startInt?
First character index to include.
Optional · Positional only
Default:start of string
endInt
Stop before this character index.
Optional · Positional only
Default:end of string
stepInt
Distance between characters. Cannot be 0; negative values read backward.
Optional · Positional only
Default:1
Returns
String — A new string slice, clamped to the available range.
label = "start start"
once = label.replace("start", "play", 1)
String.split_method
Method
Return a list of text parts split at sep. With sep=None, runs of whitespace form one separator and leading whitespace is skipped; after maxsplit splits, the rest is kept as one part.