Programming Search⌘K

Featureintro

Text

Also called Python strings, str, text in Python, string concatenation, string repetition, string indexing, immutable string.

Python keeps text in str objects. A program joins strings with +, repeats them with *, and reads characters by index or slice. A string is immutable, so a change makes a new string.

Language
Python

Synopsis

word = "Python"
print(word[0], word[-1], word[2:5])
print(3 * "un" + "ium")
print("Py" "thon", len(word))
Output
P n tho
unununium
Python 6

A string is a sequence of characters. The program reads one character by index and a substring by slice.

Semantics

r1. "Textual data in Python is handled with str objects, or strings."2 A string can be in single quotes or double quotes, "with the same result".1

r2. "The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters".1 Example: print-and-repr.

r3. "Strings can be concatenated (glued together) with the + operator, and repeated with *".1 "Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated."1 "This only works with two literals though, not with variables or expressions".1 Example: literal-join.

r4. "Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one".1 "Indices may also be negative numbers, to start counting from the right".1

r5. "Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:] is always equal to s".1 "Attempting to use an index that is too large will result in an error".1 "However, out of range slice indexes are handled gracefully when used for slicing".1 Examples: slices, index-error.

r6. "Python strings cannot be changed".1 "Therefore, assigning to an indexed position in the string results in an error".1 "If you need a different string, you should create a new one".1 Example: immutable.

r7. "The built-in function len() returns the length of a string".1

Examples

s = "First line.\nSecond line."
print(repr(s))
print(s)
Output
'First line.\nSecond line.'
First line.
Second line.

The string holds a newline character. repr() shows it as \n, and print() starts a new line (rule r2).

word = "Python"
print(word[:2] + word[2:])
print(word[-2:], word[4:42], repr(word[42:]))
Output
Python
on on ''

A slice with an index past the end gives a shorter string or an empty string (rule r5).

word = "Python"
new = "J" + word[1:]
print(new, word)
Output
Jython Python

The expression makes a new string. The old string does not change (rule r6).

Pitfalls

Assignment to a character is an error, because a string is immutable:

This code raises TypeError, on purpose.

word = "Python"
word[0] = "J"

A literal next to a variable is not joined. Use +:

This code raises SyntaxError, on purpose.

prefix = "Py"
print(prefix "thon")

An index past the end is an error:

This code raises IndexError, on purpose.

word = "Python"
word[42]

Comparison

In Go, "A string value is a (possibly empty) sequence of bytes."3 "Strings are immutable: once created, it is impossible to change the contents of a string."3 Both languages have immutable strings. An index on a Go string gives a byte, but an index on a Python string gives a string of one character (rule r4).

References

  1. The Python Tutorial, 3. An Informal Introduction to Python, 3.1.2. Text
  2. Built-in Types, Python 3.15 documentation, Text Sequence Type, str
  3. The Go Programming Language Specification, String types

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source