Week 07 String
Week 07 String
Python Programming
CT108-3-1-PYP
Complied by: Subash Khatiwada
TOPIC LEARNING OUTCOMES
This tells the interpreter, this is an apostrophe, not an opening quote to another string, so ignore it.
"abc"[1] b len("abc") 3
A 0 A
l 1 l
i 2 i
c 3 c
e 4 e
Example:
Forward 0 1 2 3 4 5 6 7 8
index
M i n e c r a f t
-9 -8 -7 -6 -5 -4 -3 -2 -1 Reverse
index
Example:
0 1 2 3 4 5 6 7 8
When SLICING, think of
the index as pointing to M i n e c r a f t
the character to the left
of the arrow -9 -8 -7 -6 -5 -4 -3 -2 -1
0 1 2 3 4 5 6 7 8
M i n e c r a f t
-9 -8 -7 -6 -5 -4 -3 -2 -1
• First Character M
firstChar = myString[0]
• Last Character
t
lastChar = myString[-1]
<expr> .method()
"abc abc".count("b") 2
>>> s = "GATTACA"
>>> s[3] = "C"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> s = s[:3] + "C" + s[4:]
>>> s
'GATCACA'
>>> s = s.replace("G","U")
>>> s
'UATCACA'
String methods do not modify the string; they return a new string.
>>> sequence = "ACGT"
>>> print(sequence.replace("A", "G"))
GCGT
>>> print(sequence)
ACGT
Or
>>> sequence = "ACGT"
>>> new_sequence = sequence.replace("A", "G")
>>> print(new_sequence)
GCGT
Returns a space-padded string with the original string centered to a total of width
columns
3 count(str, beg= 0,end=len(string))
Counts how many times str occurs in string, or in a substring of string if starting
index beg and ending index end are given
3 decode(encoding='UTF-8',errors='strict')
Decodes the string using the codec registered for encoding. encoding defaults to the
default string encoding.
4 encode(encoding='UTF-8',errors='strict')
Determines if string or a substring of string (if starting index beg and ending index
end are given) ends with suffix; Returns true if so, and false otherwise
6 expandtabs(tabsize=8)
Expands tabs in string to multiple spaces; defaults to 8 spaces per tab if tabsize not
provided
Determine if str occurs in string, or in a substring of string if starting index beg and
ending index end are given; returns index if found and -1 otherwise
8 index(str, beg=0, end=len(string))
9 isa1num()
Returns true if string has at least 1 character and all characters are alphanumeric
and false otherwise
10 isalpha()
Returns true if string has at least 1 character and all characters are alphabetic and
false otherwise
11 isdigit()
12 islower()
Returns true if string has at least 1 cased character and all cased characters are in
lowercase and false otherwise
13 isnumeric()
Returns true if a unicode string contains only numeric characters and false otherwise
14 isspace()
Returns true if string contains only whitespace characters and false otherwise
Returns true if string has at least one cased character and all cased characters are in
uppercase and false otherwise
17 join(seq)
Returns a space-padded string with the original string left-justified to a total of width
columns
20 lower()
Replaces all occurrences of old in string with new, or at most max occurrences if max
given
26 rfind(str, beg=0,end=len(string))
28 rjust(width,[, fillchar])
Returns a space-padded string with the original string right-justified to a total of width
columns.
29 rstrip()
30 split(str="", num=string.count(str))
Splits string according to delimiter str (space if not provided) and returns list of
substrings; split into at most num substrings if given
31 splitlines( num=string.count('\n'))
Splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs
removed
Python Programming Strings SLIDE 36
CT108-3-1-PYP
32 startswith(str, beg=0,end=len(string))
Determines if string or a substring of string (if starting index beg and ending index
end are given) starts with substring str; Returns true if so, and false otherwise
33 strip([chars])
34 swapcase()
35 title()
Returns "titlecased" version of string, that is, all words begin with uppercase, and the
rest are lowercase
36 translate(table, deletechars="")
Translates string according to translation table str(256 chars), removing those in the
del string
37 upper()
38 zfill (width)
Returns original string leftpadded with zeros to a total of width characters; intended
for numbers, zfill() retains any sign given (less one zero)
39 isdecimal()
Returns true if a unicode string contains only decimal characters and false otherwise
• String type
• Read/Convert
• Indexing strings []
• Slicing strings [2:4]
• Looping through strings with for and while
• Concatenating strings with +
• String operations