Method
Description
Example
s.capitalize()
Generates a copy of a
string where the first
character is capitalised, if
it is a lower-case letter.
'abc'.capitalize() #
'Abc'
'Abc'.capitalize() #
'Abc'
'1abc'.capitalize()
#'1abc'
s.count(substr,
start, end)
Counts the number of
occurrences (without
overlap) of a substring
within a larger string.
Optional index range
arguments.
text = 'Bananarama'
text.count('a') # 5
text.count('q') # 0
text.count('an') # 2
text.count('ana') # 1
s.endswith(substr,
start, end)
Determines whether a
string ends with a given
substring. Optional
position range
'xyz'.endswith('z') #
True
'xyz'.endswith('y') #
False
arguments.
'xyz'.endswith('yz')#
True
'xyz'.endswith('y',0,2)
# True
s.find(substr,
start, end)
Gives the starting index
of the first occurrence of
a substring within a
larger string, or −1 if
none is found. Optional
index range arguments.
text = 'Bananarama'
text.find('r') # 6
text.find('a') # 1
text.find('q') # -1
text.find('am') # 7
s.format(*args, **kwd)
Generates a specially
formatted version of a
string.
[See formatting in
Appendix 4
below]
s.index(substr,
start, end)
Gives the starting index
of the first occurrence of
a substring within a
larger string. Creates a
ValueError if none is
found. Optional index
range arguments.
text = 'Bananarama'
text.index('a') # 1
text.index('am') # 7
text.index('q') #
Fails!
s.isalnum()
Determines whether a
string contains only
alphanumeric characters.
Gives True if so, and
False otherwise.
'ab12'.isalnum() # True
'ab/12'.isalnum() #
False
s.isalpha()
Determines whether a
string contains only
letters of the alphabet.
'abc'.isalpha() # True
'abc?'.isalpha() #
False
s.isdigit()
Determines whether a
string contains only
numeric digit characters.
Gives True if so, and
False otherwise.
'10'.isdigit() # True
'1.0'.isdigit() # False
s.islower()
Determines whether a
string contains letters that
are all lower case. Gives
True if so, and False
otherwise.
'abc'.islower() # True
'Abc'.islower() # False
'ab@#12'.islower() #
True
'@#12'.islower() #
False
s.isspace()
Determines whether a
string contains only
whitespace characters.
Gives True if so, and
False otherwise.
' '.isspace() # True
'a'.isspace() # False
' a'.isspace() # False
'\t\n '.isspace() #
True
s.isupper()
Determines whether a
string contains letters that
are all upper case. Gives
True if so, and False
otherwise.
'ABC'.isupper () # True
'Abc'.isupper () #
False
'AB@#12'.isupper() #
True
'@#12'.isupper() #
False
s.join(iterable)
Uses one string to join
the items (which must
also be strings) that come
from a sequence
collection, or other
iterable object, to form a
new string.
x = ['G','C','A','T']
sep = ','
sep.join(x) # 'G,C,A,T'
'; '.join(x) # 'G; C;
A; T'
'/'.join(('AC','DC'))
# 'AC/DC'
s.lower()
Generates a copy of a
string where any upper-
case characters are
converted to lower case.
'Ala258'.lower() #
'ala258'
'ALA258'.lower() #
'ala258'
s.lstrip(chars)
Generates a copy of a
string where specified
characters at the start
(left) are removed.
Optional string argument
to specify which
characters to consider,
otherwise it is leading
whitespace which is
removed.
' X Y '.lstrip() # 'X
Y '
s.replace(old, new,
maxNum)
Generates one string
from another by
rna = 'AUGCAUAGCA'
dna =
replacing all occurrences
of one substring with
another substring.
Optional maximum
number of replacements.
rna.replace('U','T')
Result is 'ATGCATAGCA'.
s.rfind(substr,
start, end)
Like find(), but gives the
starting index of the last
occurrence of a substring
within a larger string, or
−1 if none is found.
text = 'Bananarama'
text.rfind('r') # 6
text.rfind('a') # 9
text.rfind('q') # -1
s.rindex(substr,
start,
end)
Like index(), but gives
the starting index of the
last occurrence of a
substring within a larger
string. Creates a
ValueError if none is
found.
text = 'Bananarama'
text.rindex('a') # 9
text.rindex('am') # 7
text.rindex('q') #
Fails!
s.rstrip( chars)
Generates a copy of a
string where specified
characters at the end
(right) are removed.
Optional string argument
to specify which
characters to consider,
otherwise it is trailing
whitespace which is
removed.
' X Y '.rstrip() # '
X Y'
s.split(sep, maxNum)
Generates a list of strings
by splitting a string into
parts where a substring is
found. If substring is not
specified then split on
whitespace.
t = 'G, C, A, T'
t.split(', ')
# ['G','C','A','T']
t.split(':')
# ['G, C, A, T']
s.splitlines(keepends)
Generates a list of strings
by splitting a string into
parts where newline
(‘\n’, ‘\r’, ‘\r\n’)
characters are found.
text = 'AC\nDC'
text.splitlines() #
['AC', 'DC']
s.startswith(prefix,
Determines whether a
string starts with a given
'xyz'.startswith('x') #
start,
end)
substring. Optional
position range
arguments.
True
'xyz'.startswith('y') #
False
'xyz'.startswith('xy')
# True
'xyz'.startswith('y',1)
# True
s.strip(chars)
Generates a copy of a
string where specified
characters at either end
are removed. Optional
string argument to
specify which characters
to consider, otherwise it
is whitespace which is
removed.
' X Y '.strip() # 'X
Y'
s.title()
Generates a copy of a
string where the first
character of each group
of letters is capitalised.
' hi joe '.title() # '
Hi Joe '
s.translate(table,
delChars)
Generates one string
from another by using a
256-character translation
table to map characters,
and optionally also delete
characters.
from string import
maketrans
table =
maketrans('U','T') # v2
rna = 'AUGCAUAGCA'
dna =
rna.translate(table)
Result is 'ATGCATAGCA'.
In Python 3 second line
instead is:
table =
str.maketrans('U','T')
s.upper()
Generates a copy of a
string where any lower-
case characters are
converted to upper case.
'ala258'.upper() #
'ALA258'
'Ala258'.upper() #
'ALA258'
s.zfill( width)
Fill a string with zero
characters ‘0’, up to a
given total width.
'7'.zfill(3) # '007'
'7'.zfill(1) # '7'
List operations
The following table describes some of the operations and inbuilt methods for Python lists
and related sequence containers, which are generally represented as seq. It should be noted
that a collection object (set, tuple, other list …) can be converted to a list using
list(collection) and that list() generates an empty list, just like[].
Operation
Description
Example
len(seq)
Determines the number of items in the
list; its length.
letters =
['G','C','A','T']
len(letters) # 4
seq[i] =
obj
Sets the item at a given index position in
a list.
letters =
['G','C','A','T']
letters[3] = 'U'
# ['G','C','A','U']
seq[i:j] =
vals
Sets a number of items covering a range
of positional indices; from a starting
positional index and up to but not
including a second index.
nums = [1,2,3,4,5,6]
nums[2:] = [3,2,1]
# [1, 2, 3, 2, 1]
vals =
seq[i:j]
Extracts number of items covering a
range of positional indices as a new list;
from a starting positional index and up
to but not including a second index.
nums = [1,2,3,4,5,6]
new1 = nums[3:]
# [4,5,6]
new2 = nums[:] # Copy
all
del seq[i:j]
Deletes an item or range of items from a
list of specified indices.
nums = [1,2,3,4,5,6]
del nums[1:5]
# [1, 6]
seq[i:j:k]
= vals
Sets a number of items covering a range
of positional indices with a given step.
myList =
[0,0,0,0,0,0]
myList[1::2] =
[1,1,1]
# [0, 1, 0, 1, 0, 1]
del
seq[i:j:k]
Deletes a number of items covering a
range of positional indices with a given
step.
letters =
['A','B','C','D','E']
del letters[::2]
# ['B', 'D']
Do'stlaringiz bilan baham: |