str¶
The items of a string are characters. There is no separate character type; a character is represented by a string of one item. Characters represent (at least) 8-bit bytes. The built-in functions chr() and ord() convert between characters and nonnegative integers representing the byte values. Bytes with the values 0-127 usually represent the corresponding ASCII values, but the interpretation of values is up to the program. The string data type is also used to represent arrays of bytes, e.g., to hold data read from a file.
Strings are immutable sequences.
Constructors¶
- str()
- Returns a string containing a printable representation of an object.
- x”string” (string designators)
- Returns a modified string.
- literal syntax
- Initializes a new instance of the str type.
Methods¶
Searching¶
- find
- Returns the index of the first occurrence of the string searched for.
- rfind
- Returns the index of the last occurrence of the string searched for.
- index
- Returns the index of the first occurrence of the string searched for (raises ValueError if not found).
- rindex
- Returns the index of the last occurrence of the string searched for (raises ValueError if not found).
Replacing¶
Leading and Trailing Characters¶
Splitting and Joining¶
- split
- Returns a list of the words in the string, separated by the delimiter string.
- rsplit
- Returns a list of the words in the string, separated by the delimiter string (starting from right).
- partition
- Returns a tuple containing the first part of the string split by the specified separator, the separator itself and the other part of the string.
- rpartition
- Returns a tuple containing the first part of the string split by the specified separator, the separator itself and the other part of the string (starting from right).
- splitlines
- Returns a list of the lines in the string, breaking at line boundaries.
- join
- Returns a string made from the elements of an iterable.
Changing Case¶
- upper
- Returns a copy of the string in UPPER CASE.
- lower
- Returns a copy of the string in lower case.
- capitalize
- Returns a copy of the string in Capital case.
- title
- Returns a copy of the string in Title Case.
- swapcase
- Returns a copy of the string with case swapped.
Information¶
- count
- Returns the number of non-overlapping occurrences of a substring in the searched string.
- startswith
- Returns a Boolean stating whether a string starts with the specified prefix.
- endswith
- Returns a Boolean stating whether a string ends with the specified suffix.
- isalnum
- Returns a Boolean stating whether the string contains only letters and digits.
- isalpha
- Returns a Boolean stating whether the string contains only letters.
- isdigit
- Returns a Boolean stating whether the string contains only digits.
- islower
- Returns a Boolean stating whether the string is in lower case.
- isspace
- Returns a Boolean stating whether the string contains only whitespace characters.
- istitle
- Returns a Boolean stating whether the string is in Title case.
- isupper
- Returns a Boolean stating whether the string is in UPPER CASE.
Formatting¶
- ljust
- Returns the string left justified in a string of specified length.
- rjust
- Returns the string right justified in a string of specified length.
- center
- Returns the string centered in a string of specified length.
- zfill
- Returns the numeric string left filled with zeros in a string of specified length.
- expandtabs
- Returns a copy of the string where all tab characters were replaced by spaces.
- `format`_
- Returns a formatted version of the string.
Functions¶
- len
- Returns an int type specifying number of elements in the collection.
- min
- Returns the smallest item from a collection.
- max
- Returns the largest item in an iterable or the largest of two or more arguments.
- cmp
- Compares two objects and returns an integer according to the outcome.
- sorted
- Returns a sorted list from the iterable.
- reversed
- Returns a reverse iterator over a sequence.
- all
- Returns a Boolean value that indicates whether the collection contains only values that evaluate to True.
- any
- Returns a Boolean value that indicates whether the collection contains any values that evaluate to True.
- enumerate
- Returns an enumerate object.
- zip
- Returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
- chr
- Returns a string of one character whose ASCII code is the specified number.
- ord
- Returns an integer representing the code of the character.
- unichr
- Returns a Unicode character specified by the code.
- `format`_
- Returns a formatted string.
Operators¶
- `% (string formatting)`_
- Formats the string according to the specified format.
- [] (index operator)
- Gives access to a sequence’s element.
- [::] (slicing)
- Gives access to a specified range of sequence’s elements.
- + (concatenation)
- Returns a concatenation of two sequences.
- * (multiple concatenation)
- Returns a sequence self-concatenated specified amount of times.
Misc¶
- Escape Characters
- List of valid escape characters.
- ASCII Table (0 - 127)
- Basic character set.