Professional Documents
Culture Documents
Week 4 Alecture
Week 4 Alecture
• Strings
The input Statement
# Model Parameters
A=35.74;B=.6215;C=-35.74;D=.4275;e=.16
# Compute and display the windchill
WC = (A+B*Temp)+(C+D*Temp)*Wind**e
print (WC)
Strings
Strings are quoted characters. Here are three
examples:
>>> s1 = ‘abc’
>>> s2 = ‘ABC’
>>> s3 = ‘ A B C ‘
s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10
Subcripting from zero creates a disconnect: ‘T’ is not the first character.
Strings are Indexed
s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10
t --> B
0
The square bracket notation is used. Note, a single character is a string.
String Slicing
s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10
t --> B e a t
0 1 2 3
We say that “t is a slice of s”.
String Slicing
s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10
t --> B e a t l e s
0 1 2 3 4 5 6
Same as s[4:11]. Handy notation when you want an “ending slice.”
String Slicing
>>> s =‘The Beatles’
>>> t = s[:4]
s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10
t --> T h e
0 1 2 3
Same as s[0:4]. Handy notation when you want a “beginning slice”.
String Slicing
>>> s =‘The Beatles’
>>> t = s[11]
IndexError: string index out of
range
s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10
The is no s[11]. An illegal to access.
Subscripting errors are EXTREMELY common.
String Slicing
>>> s =‘The Beatles’
>>> t = s[8:20]
s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10
t --> l e s
0 1 2
It is “OK” to shoot beyond the end of the source string.
Strings Can Be Combined
>>> s1 = ‘The’
>>> s2 = ‘Beatles’
>>> s = s1+s2
s --> T h e B e a t l e s
>>> s1 = ‘The’
>>> s2 = ‘Beatles’
>>> s = s1 + ‘ ‘ + s2
s --> T h e B e a t l e s
We “added” in a blank.
Python has other built-in types. And we will learn to make up our own types.
A Type is a Set of Values and
Operations on Them
Values…
Operations…
int + - * / ** %
float + - * / **
str +
concatenation
Question
Write a program that computes the result of
the given formula for three given integer (a, b
and c) numbers. You will input the numbers and
print out result of the calculation
Solution
Strings
• A string is a sequence of letters (called
characters).
• In Python, strings start and end with single or
double quotes.
>>> “foo”
‘foo’
>>> ‘foo’
‘foo’
Strings
Each string is stored in the computer’s
memory as a list of characters.
>>> myString = “GATTACA”
Question
• Input: output:
Solution
Strings
Problem
Write a program that reads the first command
line argument as a DNA sequence and prints the
first three codons, one per line, in uppercase
letters.
Question
Write a program that reads a protein sequence
as a command line argument, convert upper
case, and prints the location of the first
cysteine residue.
Question
Write a program that reads a DNA sequence
as a string and print index of the last
occurence of ‘C’
Example
Write a string decoder
that takes a special kind
of string containing two
subscripts that has a
digit and a character
sequence. These two
substrings are
separated by a delimiter
character (|).
Homework
Write a python
script that
replaces the
last occurence
of the given
substring with
another
substring.