Python Session 11-13module 2-Strings
Python Session 11-13module 2-Strings
Module 2
• Topic 1: Iteration
• Topic 2: Strings
• Topic 3: Files
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
Topic 2: Strings
2.1 A string is a sequence 2.10 Parsing strings
2.2 Getting the length of a string using len 2.11 Format operator
2.3 Traversal through a string with a loop 2.12 Debugging
2.4 String slices 2.13 Exercises
2.5 Strings are immutable
2.6 Looping and counting
2.7 The in operator
2.8 String comparison
2.9 String methods
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
In this example, we invoke find on2.9wordStringand pass the letter we are looking for as a
methods
parameter. The find method can find substrings as well as characters:
>>> word.find('na')
2
It can take as a second argument the index where it should start:
>>> word.find('na', 3)
4
One common task is to remove white space (spaces, tabs, or newlines) from the beginning
and end of a string using the strip method:
>>> line = ' Here we go '
>>> line.strip()
'Here we go'
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
2.12 Debugging
A skill that you should cultivate as you program is always asking yourself, “What could go
wrong here?” or alternatively, “What crazy thing might our user do to crash our (seemingly)
perfect program?” For example, look at the program which we used to demonstrate the
while loop in the Topic on iteration:
while True:
line = input('> ')
if line[0] == '#':
Continue
if line == 'done':
Break
print(line)
print('Done!')
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
2.12 Debugging
Look what happens when the user enters an empty line of input:
>hello there
hello there
># don't print this
>print this!
print this!
>
Traceback (most recent call last): File "copytildone.py", line 3, in <module>
if line[0] == '#': IndexError: string index out of range
The code works fine until it is presented an empty line. Then there is no zero-th character, so we get a
traceback. There are two solutions to this to make line three “safe” even if the line is empty
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
2.12 Debugging
One possibility is to simply use the startswith method which returns False if the string is
empty.
if line.startswith('#'):
Another way is to safely write the if statement using the guardian pattern and make sure the
second logical expression is evaluated only where there is at least one character in the
string.:
if len(line) > 0 and line[0] == '#':
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
2.13 Exercises
Exercise 5: Take the following Python code that stores a string:
str = 'X-DSPAM-Confidence:0.8475'
Use find and string slicing to extract the portion of the string after the colon
character and then use the float function to convert the extracted string into a
floating point number.
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
2.13 Exercises
str = 'X-DSPAM-Confidence:0.8475‘
colpos=str.find(':')
print(colpos)
18
part=str[colpos+1:]
print(part)
0.8475
part=float(part)
print(part)
0.8475