5. Computing With Strings v3
5. Computing With Strings v3
s = "abc"
len(s) → evaluates to 3
6
Strings
square brackets used to perform indexing into a string to get the value
at a certain index/position
s = "abc"
• index: 0 1 2 ← indexing always starts at 0
• index: -3 -2 -1 ← last element always at index -1
s = "hello"
s[0] = 'y' → gives an error
s = 'y'+s[1:len(s)] → is allowed, s
bound to new object
10
String Input
• Python provides input function called
input() that does not evaluate the expression that the user
types
• Example:
>>> name = input()
Ahmed
>>> print (‘hello’, name)
hello Ahmed
>>> expression = input(“enter an expression: “)
enter an expression: 3 + 2 - 1
>>> print( ‘x = ‘ + expression)
x=3+2-1
Simple ChatBot
• Believe it or not! That’s all you need to know to solve the
problem given at the beginning. Here is the code:
print( "Hi There...")
print( "What's your name?")
name = input("My name is: ")
print( "Nice to meet you " + name)
print( "How are you feeling today?")
feeling = input("I am feeling ")
print( "Glad you are feeling " + feeling )
print( "It was nice meeting you " + name+
". Hope you have a very nice day!")
print( "Have fun in CSCE 201 today ;-) ")
But there is still soooo much more
String Operations: Repetition
• Repetition builds a string by multiple
concatenations of a string with itself
• We can repeat a string using the * operator
• Example:
>>> ‘hi’ * 3
‘hihihi’
>>> ’12’ * 2
‘1212’
String Operations: Length
• Python provides a built-in function called len that returns
the number of characters in a string
• Example:
>>> name = “Bob”
>>> len (name)
3
>>> len (‘hello’)
5
• To get the last letter of a string
>>> x = len(name)
>>> print( name[x-1]) Note the -1
‘b’
String Operations: Slicing
• Slicing is used to access a substring from a string
• Slicing takes the form
• string [start : end] Including start and excluding end
• Example: -5 -4 -3 -2 -1
>>> country = ‘Egypt’ E g y p t
>>> country [1:3]
0 1 2 3 4
‘gy’
>>> country [2:]
‘ypt’ Note that the slice
>>> country [:4] stops before the end
index
‘Egyp’
>>> country [-3:4]
‘yp’
Strings
• can slice strings using [start:stop:step]
• if give two numbers, [start:stop], step=1 by default
• you can also omit numbers and leave just colons
s = "abcdefgh"
s[3:6] → evaluates to "def", same as s[3:6:1]
s[3:6:2] → evaluates to "df"
s[::] → evaluates to "abcdefgh", same as
s[0:len(s):1]
s[::-1] → evaluates to "hgfedcba", same as s[-1:- 17
(len(s)+1):-1]
s[4:1:-2] → evaluates to "ec"
for LOOPS RECAP
• for loops have a loop variable that iterates over a set of values
for varis in
• range a wayrange(4,6): → varbut
to iterate over numbers, iterates
a for over
loopvalues 4,5
<expressions>
variable can iterate over any set of values, not just numbers!
18
STRINGS AND LOOPS
• these two code snippets do the same thing
• bottom one is more “pythonic”
s = "abcdefgh"
for index in range(len(s)):
if s[index] == 'i' or s[index] == 'u’:
print("There is an i or u")
for char in s:
if char == 'i' or char == 'u’:
print("There is an i or u")
19
Exercise
s1 = "mit u rock"
s2 = "i rule mit"
if len(s1) == len(s2):
for char1 in s1:
for char2 in s2:
if char1 == char2:
print("common letter”,char2)
break
String Printing
1. Escape Characters
• Escape characters are used inside a string to
“escape” from Python’s usual syntax rules for a
moment
• Example:
>>> print( ‘she said, “that\’s hard to read”’)
she said, ”that's hard to read”
>>> print (“hello \n world”)
hello
world
String Printing
3. Formatted Printing
• To print the value of a variable with a string we can use
1. Comma-separation
>>> p = 3
>>> print (“price =“, p, “$”)
Comma-separated strings
price = 3 $ will be separated by a space
2. Concatenation
>>> name = ‘Ahmed’
>>> print (‘welcome ’ + name) Concatenation can only be
used between 2 strings
welcome Ahmed
>>> print (“price =“ + p + “$”)
TypeError: cannot concatenate 'str' and 'int‘
String Printing %d Integer
3. Formatted Printing %f
%s
Float
String
3. String formatting
>>> p = 5 >>> t = 34.5 >>> c = ‘Egypt’
>>> print (“price = %d” %(p))
price = 5
>>> print (“temp in %s = %f degrees” %(c,t))
Indicates precision
temp in Egypt = 34.500000 degrees
>>> print (“temp in %s = %.1f degrees” %(c,t))
Indicates width
temp in Egypt = 34.5 degrees
>>> print (“temp in %s = %10.1f degrees” %(c,t))
temp in Egypt = 34.5 degrees
Casting (Type Conversion)
• In Python you can convert a variable from a type to another
• To get the type of a variable use the type(var) function
• Example:
>>> x = 3
>>> type(x)
<type 'int'>
• To convert x to a float use the float(var) function
• Example:
>>> x = float(x)
>>> type(x) float (expr) Convert expr to a floating point value
<type 'float'> int (expr) Convert expr to an integer value
>>> str (1 + 3) str (expr) Return a string representation of expr
‘4'
String Representation
• Python provides built-in functions to switch between
characters and their numeric codes
• The ord() function returns the numeric “ordinal” code of a
single-character string
• Example:
>>> ord (“a”)
97 Note that ‘a’ ≠ ‘A’
>>> ord (“A”)
65
• The chr() function is the opposite of ord()
• Example: A common usage of ord and chr is in
>>> chr(97) Encryption
‘a’
Evaluating Strings
• Python provides a function eval(expr) used to
evaluate a string as an expression
• Example:
>>> eval (‘1 + 4 * 2’)
9
>>> eval (‘3.5 – 1‘)
2.5 Note that int() is used for
conversion not evaluation
>>> int (‘1+5’)
ValueError: invalid literal for int()
Evaluating Strings
• What if expression is not an expression
Note that some characters are in
• Example: the eval expression
>>> eval (‘1 + hi * 2’)
NameError: name 'hi' is not defined
TRY. EXCEPT 28
The try / except Structure
• You surround a dangerous section of code with try and
except.
• If the code in the try works - the except is skipped
• If the code in the try fails - it jumps to the except section
Will this code work
properly?
astr = 'Hello
Bob’
istr = int(astr)
print ('First', istr)
astr = '123’
istr = int(astr)
print ('Second',
istr)
astr = 'Hello Bob’
istr = int(astr)
The print 'First', istr
program astr = '123’
stops here istr = int(astr)
?
print 'Second', istr All
Done
When the first
conversion fails
astr = 'Hello Bob' - it just drops
try: into the except:
istr = int(astr) clause and the
except: program
istr = -1 continues.
Program Output
print ('First', istr)
First -1
astr = '123' Second 123
When the
try: second
istr = int(astr) conversion
except: succeeds - it
istr = -1 just skips the
except: clause
print ('Second', istr) and the
program
try /
except
astr = 'Bob' astr = 'Bob'
print 'Hello'
istr = int(astr)
istr = int(astr)
print ('There‘) print 'There'
except: istr = -1
x = int(input('Enter x>>'))
y = int(input('Enter y>>'))
Enter x>>1
try: Enter y>>0
z = x/y Handling run-time error: division by zero
except ZeroDivisionError as err:
print('Handling run-time error:',
err)
String Comparison
if word == 'banana':
print ( 'All right, bananas.‘)
http://docs.python.org/lib/string-methods.html
http://docs.python.org/lib/string-methods.html
String Library
http://docs.python.org/lib/string-methods.html
Searching a
String
• We use the find()
function to search for b a n a n a
a substring within
another string
0 1 2 3 4 5
• Sometimes we want to
take a string and
remove whitespace at >>> greet = ' Hello Bob '
the beginning and/or >>> greet.lstrip()
end 'Hello Bob '
>>> greet.rstrip()
• lstrip() and rstrip() to ' Hello Bob'
the left and right only >>> greet.strip()
'Hello Bob'
• strip() Removes both >>>
begin and ending
whitespace
Prefixes