githubEdit

Strings

Strings are sequences of characters. In Python, strings are Unicode by default, so they can represent text from many languages.

Creating Strings

single = 'Hello'
double = "Hello"
multi = """Hello,
world"""

Indexing and Slicing

text = "Python"

print(text[0])    # P
print(text[-1])   # n
print(text[1:4])  # yth

Slicing is start inclusive and end exclusive.

Immutability

Strings are immutable, so you cannot change a character in place.

text = "Hello"
# text[0] = "J"  # TypeError

Common Operations

Membership and Iteration

Formatting

Use f-strings for most cases:

str.format() is also common:

Useful Methods

Next | Previous

Last updated