Skip to content

Latest commit

 

History

History
214 lines (195 loc) · 6.97 KB

File metadata and controls

214 lines (195 loc) · 6.97 KB

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.

literal syntax

Misc

ASCII Table (0 - 127)
Basic character set.
..."" (String Designators)
Returns a modified string.
% (String Formatting Operator)
Formats the string according to the specified format.
Escape Characters
List of valid escape characters.
[] (index operator)
Gives access to a sequence's element.
[::] (slicing)
Gives access to a specified range of sequence's elements.

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.
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.

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

replace
Returns a copy of the string with a specified substring replaced specified number of times.
translate
Returns a copy of the string with characters mapped through the given translation table or deleted.

Leading and Trailing Characters

lstrip
Returns a copy of the string with leading characters removed.
rstrip
Returns a copy of the string with trailing characters removed.
strip
Returns a copy of the string with leading and trailing characters removed.

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.

Encodings

decode
Decodes the string using the codec registered for encoding.
encode
Returns an encoded version of the string.