0% found this document useful (0 votes)
5 views37 pages

Python IIteration Strings

Python_IIteration_Strings

Uploaded by

eeenba c5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views37 pages

Python IIteration Strings

Python_IIteration_Strings

Uploaded by

eeenba c5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

8.

Iteration: Strings

Topics:

Using Methods from the string class

Iterating through a string with for


Iterating Through a String
Two problems we cannot easily solve:

1. Given a string s, assign to t the “reversed”


string. ‘abcd’ → ‘dcba’

2. Given a string s, how many digit characters


does it contain? ‘1or2or3’ → 3
The Reverse String Problem

s -> ‘abcd’
s = ‘abcd’
t = ‘’
for c in s: t -> ‘dcba’
t = c + t

How does the for loop work?


The Number-of-Digits Problem

s = ‘2x78y’ s -> ‘2x78y’


n = 0
for c in s:
if [Link](): n -> 3
n=n+1

How does the for loop work?


Using for to Traverse a String
Character-by-Character

Output:
s = ‘abcd’
a
for c in s: b
Print(c) c
d

In this example, the “for-loop” variable is c. One


at a time, it takes on the value of each character in
s.
The Reverse String Problem

s = ‘abcd’ s -> ‘abcd’


t = ‘’ ‘’
t ->
for c in s:
t = c + t
Print(t) ‘abcd’
c -> ‘a’

At the start of the loop, c is assigned the zeroth


character in s.
The Reverse String Problem

s = ‘abcd’ s -> ‘abcd’


t = ‘’
t -> ‘’
for c in s:
t = c + t
Print(t) ‘abcd’
c -> ‘a’

The loop body is executed using that value in c.


The Reverse String Problem

s = ‘abcd’ s -> ‘abcd’


t = ‘’ ‘a’
t ->
for c in s:
t = c + t
Print(t) ‘abcd’
c -> ‘a’

The loop body is executed using that value in c.


The Reverse String Problem

s -> ‘abcd’
s = ‘abcd’
t = ‘’ t -> ‘a’
for c in s:
t = c + t
‘abcd’
Print(t)
c -> ‘b’

The next time through the loop, c is assigned


the first character in s.
The Reverse String Problem

s -> ‘abcd’
s = ‘abcd’
t = ‘’ t -> ‘a’
for c in s:
t = c + t
‘abcd’
Print(t)
c -> ‘b’

The loop body is executed using that value in c.


The Reverse String Problem

s -> ‘abcd’
s = ‘abcd’
t = ‘’ t -> ‘ba’
for c in s:
t = c + t
‘abcd’
Print(t)
c -> ‘b’

The loop body is executed using that value in c.


The Reverse String Problem

s -> ‘abcd’
s = ‘abcd’
t = ‘’ t -> ‘ba’
for c in s:
t = c + t
‘abcd’
Print(t)
c -> ‘c’

The next time through the loop, c is assigned


the second character in s.
The Reverse String Problem

s -> ‘abcd’
s = ‘abcd’
t = ‘’ t -> ‘cba’
for c in s:
t = c + t
‘abcd’
Print(t)
c -> ‘c’

The loop body is executed using that value in c.


The Reverse String Problem

s = ‘abcd’ s -> ‘abcd’


t = ‘’ ‘ba’
t ->
for c in s:
t = c + t
Print(t) ‘abcd’
c -> ‘c’

The loop body is executed using that value in c.


The Reverse String Problem

s -> ‘abcd’
s = ‘abcd’
t = ‘’ t -> ‘cba’
for c in s:
t = c + t
‘abcd’
Print(t)
c -> ‘d’

The last time through the loop, c is assigned


the third character in s.
The Reverse String Problem

s -> ‘abcd’
s = ‘abcd’
t = ‘’ t -> ‘cba’
for c in s:
t = c + t
‘abcd’
Print(t)
c -> ‘d’

The loop body is executed using that value in c.


The Reverse String Problem

s -> ‘abcd’
s = ‘abcd’
t = ‘’ t -> ‘dcba’
for c in s:
t = c + t
‘abcd’
Print(t)
c -> ‘d’

The loop body is executed using that value in c.


The Reverse String Problem

s -> ‘abcd’
s = ‘abcd’
t = ‘’ t -> ‘dcba’
for c in s:
t = c + t
Print(t) Output: dcba

The string has been traversed. The iteration ends.


The next statement after the loop is executed.
Indentation important.
for-loop Mechanics
for <loop variable> in <string>:

Loop Body

If the string has length n, then the loop body is


executed n times.
for-loop Mechanics
for x in y:

Loop Body

Let x = y[0] and then execute the loop body.


Let x = y[1] and then execute the loop body.
Let x = y[2] and then execute the loop body.
etc
Let x = y[n-1] and then execute the loop body.
Function for Reversing Strings
def Reverse(s):
""" Returns a string that is obtained
from s by reversing the order of its
characters.

Precondition: s is a string."""

t = ‘’ # The empty string


for c in s:
t = c+t # Repeated concatenation
return t
The Number-of-Digits Problem

Given a string s, how many of its


characters are digit characters?

‘a10b20c30d40’ → 8
The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 0
n=n+1
print(n) ‘2z78y’
x -> ‘2’

At the start of the loop, x is assigned the zeroth


character in s.
The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 0
n=n+1
Print(n) ‘2z78y’
x -> ‘2’

The loop body is executed using that value in x.


The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 1
n=n+1
Print(n) ‘2z78y’
x -> ‘2’

The loop body is executed using that value in x.


The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 1
n=n+1
Print(n) ‘2z78y’
x -> ‘z’

The next time through the loop, x is assigned the


first character in s.
The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 1
n=n+1
Print(n) ‘2z78y’
x -> ‘z’

The loop body is executed using that value in x.


The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 1
n=n+1
Print(n) ‘2z78y’
x -> ‘7’

The next time through the loop, x is assigned the


second character in s.
The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 1
n=n+1
Print(n) ‘2z78y’
x -> ‘7’

The loop body is executed using that value in x.


The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 2
n=n+1
Print(n) ‘2z78y’
x -> ‘7’

The loop body is executed using that value in x.


The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 2
n=n+1
Print(n) ‘2z78y’
x -> ‘8’

The next time through the loop, x is assigned


the third character in s.
The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 2
n=n+1
Print(n) ‘2z78y’
x -> ‘8’

The loop body is executed using that value in x.


The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 3
n=n+1
Print(n) ‘2z78y’
x -> ‘8’

The loop body is executed using that value in x.


The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 3
n=n+1
Print(n) ‘2z78y’
x -> ‘y’

The next time through the loop, x is assigned


the fourth character in s.
The Number-of-Digits Problem

s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 3
n=n+1
Print(n) ‘2z78y’
x -> ‘y’

The loop body is executed using that value in x.


The Number-of-Digits Problem
s = ‘2z78y’
n = 0 s -> ‘2z78y’
for x in s:
if [Link](): n -> 3
n=n+1

Print(n) Output:
3

The string has been traversed. The iteration ends.


The next statement after the loop is executed.
Indentation important.
Function for Counting Digits
def nDigits(s):
""" Returns an int whose value is the
number of digit characters that are in
s.

Precondition: s is a string."""
n = 0;
for c in s:
# Increment n if c is a digit
if [Link]():
n=n+1
return n

You might also like