Search notes:

Python: str (string)

A str (string) is a immutable sequence of Unicode code points.

Members of str

A list of members of str can be produced with dir:
for e in dir(str): print(e)
__add__
__class__
__contains__
__delattr__
__dir__
__doc__
__eq__
__format__
__ge__
__getattribute__
__getitem__
__getnewargs__
__gt__
__hash__
__init__
__init_subclass__
__iter__ String is an iterable (which allows for to iterate over a string's characters).
__le__
__len__
__lt__
__mod__
__mul__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__rmod__
__rmul__
__setattr__
__sizeof__
__str__
__subclasshook__
capitalize, lower, upper, swapcase
casefold
center
count
encode() returns a bytes object (encoding can be specified, is UTF-8 by default)
endswith, startswith Checks if the string starts with or ends with a given string.
expandtabs
find, rfind
format
format() probably what comes closest to the printf function of the C standard library.
format_map
index, rindex
isalnum
isalpha
isascii
isdecimal
isdigit
isidentifier
islower
isnumeric
isprintable
isspace
istitle
isupper
join
ljust, rjust left/right alignment of a string. Compare with zfill
lstrip
maketrans Used in conjunction with translate(…).
partition, rpartition
removeprefix
removesuffix
replace(old, new) replaces the text sequence old with new in str.
str.rstrip() allows to remove aribtrary characters from the string's right side and thus can be used (almost) like Perl's chomp() function.
split, rsplit Returns a list from the parts of the original string that are separated by the separator.
splitlines() Splits a text on \r, \n or \r\n (i. e. on the traditional Unix, DOS and Mac line breaks).
strip
title
translate(…)
zfill left-pads a string with zeroes: print(str(42).zfill(5)) prints 00042. Use str(42).rjust(5, ' ') to left pad the number with spaces.

Misc

Taking substrings from a string, for example to remove the last n characters from a string.
The built-in functions int() and float() can be used to convert a string to an integer and float, respectively.
Similarly, a numerical value can be converted to a string with str().

Strings are immutable

Strings are immutable. Thus, it is not possible to modify a string in place.
But see io.StringIO() for a way to somewhat simulate such a requirement.

Embedding expressions in a string

f-strings allow to embed expressions in a string. The expreassion is evaluated when the string is created, not when the string is evaluated:
>>> a = 2
>>> b = 3
>>> t = f"result = {a+b}"
>>> t
'result = 5'
>>> a=9
>>> t
'result = 5'

Repeating strings n times

A string is repeated n times with the multiplication operator ('text' * 5).

Reversing a string

A string can be reversed with the slicing notation [::-1]:
>>> 'abcdef'[::-1]
'fedcba'

Create a (simple!) regular expression to parse CSV files

nofFields = 10
csvregex = ';'.join(['([^;]*)'] * nofFields)

See also

Another object that is immutable is the bytearray object.
txt.endswith(suffix) returns true if txt ends with the string that is stored in the variable suffix.
The standard library string
Other Built-in types

Index