User Tools

Site Tools


python_strings

Python Strings

Python Strings

Strings

The normal str string object is an immutable (unchangeable) sequence of characters accessed by offset (position). Its characters are code point ordinals in the underlying character set, and individual characters are string objects of length 1.

The full string object model varies across lines.

Python 3.X has three string types with similar interfaces:

str

An immutable sequence of characters, used for all text — both ASCII and richer Unicode.

bytes

An immutable sequence of short integers, used for the byte values of binary data.

bytearray

A mutable variant of bytes.

Python 2.X instead has two string types with similar interfaces:

str

An immutable sequence of characters, used for both byte oriented (8-bit) text and binary data.

unicode

An immutable sequence of characters, used for possibly-richer Unicode text.

Python 2.X (as of 2.6) also has the Python 3.X bytearray type as a back-port from 3.X, but it does not impose as sharp a distinction between text and binary data. (It may be mixed with text strings freely in 2.X.)

For Unicode support in both 3.X and 2.X, see Unicode Strings. Most of the remainder of this section pertains to all string types, but see String methods, Unicode Strings, and Built-in Functions for more on bytes and bytearray.

python_strings.txt · Last modified: 2024/04/28 03:21 (external edit)