Introduction to
“Computing with Strings”
Outline
• What is a String?
• String Operations
• Concatenation
• Repetition
• Indexing
• Length
• Slicing
• String Printing
• Escape characters
• Multiline string
• Formatted printing
Outline - cont
• String Input
• Casting
• Evaluating Strings
• String Representation – (Ord and chr)
• The String Library
Why Strings?
• Many programs depend on string usage and
manipulation
• An example problem:
• Write a program to emulate an intelligent
ChatBot…
• To solve a problem like that, you need to learn
about strings
What is a String?
• A string is a sequence of characters
• Strings are delimited by single ‘ ‘ or double “ “
quotes
• Strings can be stored in variables
• Example:
>>> Str1 = “hello”
>>> Name = ‘Ahmed’
>>> ‘that’ll not work’
SyntaxError: invalid syntax
>>> “that’ll work”
What if you want to put both kinds of quote in one string?
Strings
• think of as a sequence of case sensitive characters
• can compare strings with ==, >, < etc.
• len() is a function used to retrieve the length of the string in
the parentheses
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[0] → evaluates to "a"
s[1] → evaluates to "b"
s[2] → evaluates to "c"
s[3] → trying to index out of bounds, error
s[-1] → evaluates to "c"
7
s[-2] → evaluates to "b"
s[-3] → evaluates to "a"
String Operations: Indexing
• Indexing is used to access the individual characters that make
up the string
• Characters in a string are indexed from 0 to the length of the
string - 1
• Example:
>>> greet = ‘Hello Bob’
>>>greet[1]
‘e’
>>>greet[9]
IndexError: string index out of range
• Note: Strings are immutable i.e. you can not edit a string
• Example:
>>> greet[0] = ‘p’
TypeError: 'str' object does not support item assignment
String Operations:
1. Concatenation
• The + operator can be used to join 2 strings
Note the
• Example: space here
>>> print (‘hello ’ + “world”)
hello world
>>> print (‘1’ + ‘2’)
12
>>> Fname = ‘Foo’
>>> print (‘Welcome ’ + Fname)
Welcome Foo
Strings
• strings are “immutable” – cannot be modified
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[Link]
s[Link] → 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[Link]-2] → evaluates to "ec"
for LOOPS RECAP
• for loops have a loop variable that iterates over a set of values
for var in range(4): var iterates over values 0,1,2,3
expressions inside loop executed
<expressions>
with each value for var
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'
try: print 'Hello'
print 'Hello'
istr = int(astr)
istr = int(astr)
print ('There‘) print 'There'
except: istr = -1
istr = -1 print 'Done', istr Safety net
print ('Done', istr)
Sample try / except
rawstr = input('Enter a
number:')
try: Sample Run 1
ival = int(rawstr) Enter a number:42
except: Nice work
ival = -1
Sample Run 2
if ival > 0 : Enter a number:fourtytwo
print ('Nice work‘) Not a number
else:
print ('Not a number‘)
Sample try / except
Division by Zero
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.‘)
if word < 'banana':
print ('Your word,' + word + ', comes before banana.’)
elif word > 'banana':
print ('Your word,' + word + ', comes after banana.’)
else:
print ('All right, bananas.‘)
String Library
• Python has a number of
string functions which are in
the string library
>>> greet = 'Hello Bob‘
• These functions are already >>> zap = [Link]()
>>> print zap
built into every string - we
invoke them by appending hello bob
the function to the string >>> print greet
variable Hello Bob
>>> print 'Hi There'.lower()
• These functions do not hi there
modify the original string, >>>
instead they return a new
string that has been altered
The String Methods
>>> stuff = 'Hello world’
>>> type(stuff)<type 'str'>
>>> dir(stuff)
['capitalize', 'center', 'count', 'decode', 'encode',
'endswith', 'expandtabs', 'find', 'format', 'index',
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition',
'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit',
'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase',
'title', 'translate', 'upper', 'zfill']
[Link]
[Link]
String Library
[Link]() [Link](old, new[, count])
[Link](width[, fillchar]) [Link]()
[Link](suffix[, start[, end]]) [Link]([chars])
[Link](sub[, start[, end]]) [Link]([chars])
[Link]([chars]) [Link]()
[Link]
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
• find() finds the first >>>
>>>
fruit = 'banana'
pos = [Link]('na')
occurance of the
substring >>> print pos
2
• If the substring is not >>>
>>>
aa = [Link]('z')
print aa
found, find() returns -1
-1
• Remember that string
position starts at zero
Making everything UPPER
CASE
• You can make a copy of a >>> greet = 'Hello Bob'
string in lower case or upper >>> nnn = [Link]()
case >>> print nnn
HELLO BOB
• Often when we are searching
>>> www = [Link]()
for a string using find() - we
>>> print www
first convert the string to
hello bob
lower case so we can search a
>>>
string regardless of case
Search and Replace
• The replace()
function is like a
“search and
replace” >>> greet = 'Hello Bob'
operation in a >>> nstr = [Link]('Bob','Jane')
word processor >>> print nstr
Hello Jane
• It replaces all >>> nstr = [Link]('o','X')
occurrences of >>> print nstrHellX BXb
the search string >>>
with the
replacement
string
Stripping Whitespace
• Sometimes we want to
take a string and
remove whitespace at >>> greet = ' Hello Bob '
the beginning and/or >>> [Link]()
end 'Hello Bob '
>>> [Link]()
• lstrip() and rstrip() to ' Hello Bob'
the left and right only >>> [Link]()
'Hello Bob'
• strip() Removes both >>>
begin and ending
whitespace
Prefixes
>>> line = 'Please have a nice day’
>>> [Link]('Please')
True
>>> [Link]('p')
False
21 31
From [Link]@[Link] Sat Jan 5 [Link] 20
>> data = 'From [Link]@[Link] Sat Jan 5 [Link] 200
>> atpos = [Link]('@')
>> print atpos
1
>> sppos = [Link](' ',atpos)
>> print sppos
1
>> host = data[atpos+1 : sppos]
>> print host Parsing and
[Link] Extracting
Exercise
• s1 = ‘welcome to’ s2 = ‘CSCE’ s3 = ‘201’
• Show the result of each of the following:
1. print (s1 + s2, s3)
2. print (s3*3)
3. print (int(s3)*3)
4. print (s1[2:5])
5. print ([Link]())
6. print ([Link](’s’))
7. print (s1+’\\’+s2+’\t’+s3)