1 string processing samuel marteck ©2010. 2 a character in a string can be accessed by indicating...

Post on 21-Dec-2015

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

String processing

Samuel Marteck ©2010

2

A character in a string can be accessed by

indicating its subscript, also called index .

For instance, how do you get the ‘c’ in

s =‘ac453’ ?

3

character ‘a’ ‘c’ ‘4’ ‘5’ ‘3’

index 0 1 2 3 4

4

How do you get the ‘c’ in s =‘ac453’ ?

S[1] = ‘c’

What is the index of the ‘3, the last

character in the string?

5

character ‘a’ ‘c’ ‘4’ ‘5’ ‘3’

index 0 1 2 3 4

6

s =‘ac453’ ?

What is the index of the ‘3, the last

character in the string?

It’s 4, one less than the length of the string

7

The following prints the characters in a string:

8

s = 'abcd'

length = len(s)

for j in range(length): # 0 <= j <= length -1 print(s[j]) #prints each character in s

9

s = 'abcd' length = len(s) for j in range(length): # 0 <= j <= length -1

print(s[j]) #prints each character in s

Since the index of the last character in s is 3and the last value of the loop index is also 3, no error occurs.

10

For s = 'abcd‘, the length is 4. What would

s[4] produce?

11

For s = 'abcd‘, the length is 4. What would

s[4] produce?

It produces an error message:

IndexError: string index out of range

12

In s = 'abcd‘, can you replace the ‘c’ with a

‘z’ by writing s[2] = ‘z’ ?

13

In s = 'abcd‘, can you replace the ‘c’ with a

‘z’ by writing s[2] = ‘z’ ?

The answer is no.

How would you write a function that replaces

every occurrence of a ‘c’ in string with a ‘z’?

14

Let’s write a function to determine if every

letter in a word is lowercase.

15

def islower(s):

length = len(s)

for j in range(length):

if not( 'a' <=s[j] <= 'z' ):

return False

return True

When does this return True?

16

def islower(s): length = len(s) for j in range(length): if not( 'a' <=s[j] <= 'z' ): return False return TrueWhen does this return True?Only after the entire loop is finished and no non-lowercase letters are found.

17

Why is the following wrong?

def islower(s): length = len(s) for j in range(length): if not( 'a' <=s[j] <= 'z' ): return False else: return True

18

def islower(s): length = len(s) for j in range(length): if not( 'a' <=s[j] <= 'z' ): return False else: return TrueThe first time a lowercase letter is found, the function returns True, even if a non- lowercase letter follows it.

19

Checking if ‘asdfdsa’ is a palindrome. Is it

symmetric about the middle character?

20

Checking if ‘asdfdsa’ is a palindrome. Its

length is 7.

21

Checking if s =‘asdfdsa’

is a palindrome. len(s) =

7

j S[j] k =len-1-j S[k]

0 ‘a’ 6 ‘a’

22

Checking if s =‘asdfdsa’

is a palindrome. len(s) =

7

j S[j] k =len-1-j S[k]

0 ‘a’ 6 ‘a’

1 ‘s’ 5 ‘s’

23

The program is:def palin(s): length = len(s) for j in range(length//2): if s[j ] != s[length - j - 1]: return False #quits the function return True #quits when loop is finished def main(): s = input('type your string \n') if palin(s): print(s + ' is a plaindrome') else: print(s + ' is not a plaindrome') main()

24

Write a function that has a 3-digit int

parameter, returns the digit that is the

middle one according to size.

25

• def ismiddle(n):#returns middle digit according to size• s = str(n) • a = int(s[0])• b = int(s[1])• c = int(s[2])• small = min(a, b, c )• big = max(a, b, c)• sum = a + b + c• middle = sum - big - small• return middle

26

substrings

A substring is a part of a string upto and

including the string itself.

27

If s = ‘asdfg’, to get the entire string, use

s[0:len(s)].

28

Here len(s) is 5, so

s[0:5] gives you the

entire string. Note that

the 2nd parameter

(here 5) is one more than

the index of the

last character in the desired

string

character ‘a’ ‘s’ ‘d’ ‘f’ ‘g’

index 0 1 2 3 4

29

What would s[1:4] give?

30

character ‘a’ ‘s’ ‘d’ ‘f’ ‘g’

index 0 1 2 3 4

31

S[2:4] → ‘sdf’

What is S[2:2]?

32

What is S[2:2]?

It’s the empty string since the string

character indices go from 2 to 1.

33

You can number the characters from the

back of the string starting with -1 as is

shown next

34

character ‘a’ ‘s’ ‘d’ ‘f’ ‘g’

index -5 -4 -3 -2 -1

35

• How would you write a function to print a string in reverse?

36

You want:

• The initial value of the loop index to be -1,

• The increment to be -1

• The final value to be the negative of one more than the length since the initial value is -1 and not 0.

37

• def reverse(s):#prints a string in reverse

• n = len(s)

• for j in range(-1, -(n+1), -1):

• print(s[j])

38

Write a program that produces a triangle.

39

We’ll start with s = ‘xxxxxxxx’.

S[0:1] will give the character at the string

index = 0.

The entire 8-character string is represented

by s[0:9]

How do we write the range in the for loop?

40

The entire 8-character string is represented

by s[0:9]

How do we write the range in the for loop?

41

The entire 8-character string is represented by s[0:9]How do we write the range in the for loop?

for j in range(1:10)

since the maximum value of j is one less than 10;but the length is 9. How do we write the program?

42

• def triangle(s):#produces a triangle

• n = len(s)

• for j in range(1, n+1):

• print(s[0:j])

43

• In s[ :len(s)] since the first parameter is blank, it’s assumed that it is 0. So it’s equivalent to s[0 :len(s)].

• In s[2: ] since the second parameter is blank, it’s assumed it is the length of the string. So it’s equivalent to s[2 :len(s)].

44

If the second parameter is greater than the

length of the string, the second parameter is

set to the length of the string. So for a string

of length 5, s[1: 10] is equivalent to s[1: 5].

top related