Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Startswith (Startswith (: Bytes-Like Object

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

7/14/2020 Built-in Types — Python 3.8.

4 documentation

The separator to search for may be any bytes-like object.

bytes. startswith(prefix[, start[, end]])


bytearray. startswith(prefix[, start[, end]])
Return True if the binary data starts with the specified prefix, otherwise return False . prefix can
also be a tuple of prefixes to look for. With optional start, test beginning at that position. With
optional end, stop comparing at that position.

The prefix(es) to search for may be any bytes-like object.

bytes. translate(table, /, delete=b'')


bytearray. translate(table, /, delete=b'')
Return a copy of the bytes or bytearray object where all bytes occurring in the optional argument
delete are removed, and the remaining bytes have been mapped through the given translation
table, which must be a bytes object of length 256.

You can use the bytes.maketrans() method to create a translation table.

Set the table argument to None for translations that only delete characters:

>>> b'read this short text'.translate(None, b'aeiou') >>>


b'rd ths shrt txt'

Changed in version 3.6: delete is now supported as a keyword argument.

The following methods on bytes and bytearray objects have default behaviours that assume the use
of ASCII compatible binary formats, but can still be used with arbitrary binary data by passing
appropriate arguments. Note that all of the bytearray methods in this section do not operate in place,
and instead produce new objects.

bytes. center(width[, fillbyte])


bytearray. center(width[, fillbyte])
Return a copy of the object centered in a sequence of length width. Padding is done using the
specified fillbyte (default is an ASCII space). For bytes objects, the original sequence is
returned if width is less than or equal to len(s) .

Note: The bytearray version of this method does not operate in place - it always produces a
new object, even if no changes were made.

bytes. ljust(width[, fillbyte])


bytearray. ljust(width[, fillbyte])
Return a copy of the object left justified in a sequence of length width. Padding is done using the
specified fillbyte (default is an ASCII space). For bytes objects, the original sequence is
returned if width is less than or equal to len(s) .

https://docs.python.org/3/library/stdtypes.html#old-string-formatting 40/74
7/14/2020 Built-in Types — Python 3.8.4 documentation

Note: The bytearray version of this method does not operate in place - it always produces a
new object, even if no changes were made.

bytes. lstrip([chars])
bytearray. lstrip([chars])
Return a copy of the sequence with specified leading bytes removed. The chars argument is a
binary sequence specifying the set of byte values to be removed - the name refers to the fact
this method is usually used with ASCII characters. If omitted or None , the chars argument
defaults to removing ASCII whitespace. The chars argument is not a prefix; rather, all
combinations of its values are stripped:

>>> b' spacious '.lstrip() >>>


b'spacious '
>>> b'www.example.com'.lstrip(b'cmowz.')
b'example.com'

The binary sequence of byte values to remove may be any bytes-like object.

Note: The bytearray version of this method does not operate in place - it always produces a
new object, even if no changes were made.

bytes. rjust(width[, fillbyte])


bytearray. rjust(width[, fillbyte])
Return a copy of the object right justified in a sequence of length width. Padding is done using
the specified fillbyte (default is an ASCII space). For bytes objects, the original sequence is
returned if width is less than or equal to len(s) .

Note: The bytearray version of this method does not operate in place - it always produces a
new object, even if no changes were made.

bytes. rsplit(sep=None, maxsplit=-1)


bytearray. rsplit(sep=None, maxsplit=-1)
Split the binary sequence into subsequences of the same type, using sep as the delimiter string.
If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified
or None , any subsequence consisting solely of ASCII whitespace is a separator. Except for
splitting from the right, rsplit() behaves like split() which is described in detail below.

bytes. rstrip([chars])
bytearray. rstrip([chars])
Return a copy of the sequence with specified trailing bytes removed. The chars argument is a
binary sequence specifying the set of byte values to be removed - the name refers to the fact
this method is usually used with ASCII characters. If omitted or None , the chars argument

https://docs.python.org/3/library/stdtypes.html#old-string-formatting 41/74
7/14/2020 Built-in Types — Python 3.8.4 documentation

defaults to removing ASCII whitespace. The chars argument is not a suffix; rather, all
combinations of its values are stripped:

>>> b' spacious '.rstrip() >>>


b' spacious'
>>> b'mississippi'.rstrip(b'ipz')
b'mississ'

The binary sequence of byte values to remove may be any bytes-like object.

Note: The bytearray version of this method does not operate in place - it always produces a
new object, even if no changes were made.

bytes. split(sep=None, maxsplit=-1)


bytearray. split(sep=None, maxsplit=-1)
Split the binary sequence into subsequences of the same type, using sep as the delimiter string.
If maxsplit is given and non-negative, at most maxsplit splits are done (thus, the list will have at
most maxsplit+1 elements). If maxsplit is not specified or is -1 , then there is no limit on the
number of splits (all possible splits are made).

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit
empty subsequences (for example, b'1,,2'.split(b',') returns [b'1', b'', b'2'] ). The
sep argument may consist of a multibyte sequence (for example, b'1<>2<>3'.split(b'<>')
returns [b'1', b'2', b'3'] ). Splitting an empty sequence with a specified separator returns
[b''] or [bytearray(b'')] depending on the type of object being split. The sep argument may
be any bytes-like object.

For example:

>>> b'1,2,3'.split(b',') >>>


[b'1', b'2', b'3']
>>> b'1,2,3'.split(b',', maxsplit=1)
[b'1', b'2,3']
>>> b'1,2,,3,'.split(b',')
[b'1', b'2', b'', b'3', b'']

If sep is not specified or is None , a different splitting algorithm is applied: runs of consecutive
ASCII whitespace are regarded as a single separator, and the result will contain no empty
strings at the start or end if the sequence has leading or trailing whitespace. Consequently,
splitting an empty sequence or a sequence consisting solely of ASCII whitespace without a
specified separator returns [] .

For example:

>>> b'1 2 3'.split() >>>


[b'1', b'2', b'3']

https://docs.python.org/3/library/stdtypes.html#old-string-formatting 42/74
7/14/2020 Built-in Types — Python 3.8.4 documentation

>>> b'1 2 3'.split(maxsplit=1)


[b'1', b'2 3']
>>> b' 1 2 3 '.split()
[b'1', b'2', b'3']

bytes. strip([chars])
bytearray. strip([chars])
Return a copy of the sequence with specified leading and trailing bytes removed. The chars
argument is a binary sequence specifying the set of byte values to be removed - the name
refers to the fact this method is usually used with ASCII characters. If omitted or None , the chars
argument defaults to removing ASCII whitespace. The chars argument is not a prefix or suffix;
rather, all combinations of its values are stripped:

>>> b' spacious '.strip() >>>


b'spacious'
>>> b'www.example.com'.strip(b'cmowz.')
b'example'

The binary sequence of byte values to remove may be any bytes-like object.

Note: The bytearray version of this method does not operate in place - it always produces a
new object, even if no changes were made.

The following methods on bytes and bytearray objects assume the use of ASCII compatible binary
formats and should not be applied to arbitrary binary data. Note that all of the bytearray methods in
this section do not operate in place, and instead produce new objects.

bytes. capitalize()
bytearray. capitalize()
Return a copy of the sequence with each byte interpreted as an ASCII character, and the first
byte capitalized and the rest lowercased. Non-ASCII byte values are passed through
unchanged.

Note: The bytearray version of this method does not operate in place - it always produces a
new object, even if no changes were made.

bytes. expandtabs(tabsize=8)
bytearray. expandtabs(tabsize=8)
Return a copy of the sequence where all ASCII tab characters are replaced by one or more
ASCII spaces, depending on the current column and the given tab size. Tab positions occur
every tabsize bytes (default is 8, giving tab positions at columns 0, 8, 16 and so on). To expand
the sequence, the current column is set to zero and the sequence is examined byte by byte. If
the byte is an ASCII tab character ( b'\t' ), one or more space characters are inserted in the
result until the current column is equal to the next tab position. (The tab character itself is not

https://docs.python.org/3/library/stdtypes.html#old-string-formatting 43/74
7/14/2020 Built-in Types — Python 3.8.4 documentation

copied.) If the current byte is an ASCII newline ( b'\n' ) or carriage return ( b'\r' ), it is copied
and the current column is reset to zero. Any other byte value is copied unchanged and the
current column is incremented by one regardless of how the byte value is represented when
printed:

>>> b'01\t012\t0123\t01234'.expandtabs() >>>


b'01 012 0123 01234'
>>> b'01\t012\t0123\t01234'.expandtabs(4)
b'01 012 0123 01234'

Note: The bytearray version of this method does not operate in place - it always produces a
new object, even if no changes were made.

bytes. isalnum()
bytearray. isalnum()
Return True if all bytes in the sequence are alphabetical ASCII characters or ASCII decimal
digits and the sequence is not empty, False otherwise. Alphabetic ASCII characters are those
byte values in the sequence b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' .
ASCII decimal digits are those byte values in the sequence b'0123456789' .

For example:

>>> b'ABCabc1'.isalnum() >>>


True
>>> b'ABC abc1'.isalnum()
False

bytes. isalpha()
bytearray. isalpha()
Return True if all bytes in the sequence are alphabetic ASCII characters and the sequence is
not empty, False otherwise. Alphabetic ASCII characters are those byte values in the sequence
b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' .

For example:

>>> b'ABCabc'.isalpha() >>>


True
>>> b'ABCabc1'.isalpha()
False

bytes. isascii()
bytearray. isascii()
Return True if the sequence is empty or all bytes in the sequence are ASCII, False otherwise.
ASCII bytes are in the range 0-0x7F.

https://docs.python.org/3/library/stdtypes.html#old-string-formatting 44/74
7/14/2020 Built-in Types — Python 3.8.4 documentation

New in version 3.7.

bytes. isdigit()
bytearray. isdigit()
Return True if all bytes in the sequence are ASCII decimal digits and the sequence is not empty,
False otherwise. ASCII decimal digits are those byte values in the sequence b'0123456789' .

For example:

>>> b'1234'.isdigit() >>>


True
>>> b'1.23'.isdigit()
False

bytes. islower()
bytearray. islower()
Return True if there is at least one lowercase ASCII character in the sequence and no
uppercase ASCII characters, False otherwise.

For example:

>>> b'hello world'.islower() >>>


True
>>> b'Hello world'.islower()
False

Lowercase ASCII characters are those byte values in the sequence


b'abcdefghijklmnopqrstuvwxyz' . Uppercase ASCII characters are those byte values in the
sequence b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .

bytes. isspace()
bytearray. isspace()
Return True if all bytes in the sequence are ASCII whitespace and the sequence is not empty,
False otherwise. ASCII whitespace characters are those byte values in the sequence b'
\t\n\r\x0b\f' (space, tab, newline, carriage return, vertical tab, form feed).

bytes. istitle()
bytearray. istitle()
Return True if the sequence is ASCII titlecase and the sequence is not empty, False otherwise.
See bytes.title() for more details on the definition of “titlecase”.

For example:

>>> b'Hello World'.istitle() >>>


True

https://docs.python.org/3/library/stdtypes.html#old-string-formatting 45/74

You might also like