Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 String processing Samuel Marteck ©2010

2 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 3 character‘a’‘c’‘4’‘5’‘3’ index01234

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 5 character‘a’‘c’‘4’‘5’‘3’ index01234

6 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 7 The following prints the characters in a string:

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

9 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 3 and the last value of the loop index is also 3, no error occurs.

10 10 For s = 'abcd‘, the length is 4. What would s[4] produce?

11 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 12 In s = 'abcd‘, can you replace the ‘c’ with a ‘z’ by writing s[2] = ‘z’ ?

13 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 14 Let’s write a function to determine if every letter in a word is lowercase.

15 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 16 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? Only after the entire loop is finished and no non- lowercase letters are found.

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

19 19 Checking if ‘asdfdsa’ is a palindrome. Is it symmetric about the middle character?

20 20 Checking if ‘asdfdsa’ is a palindrome. Its length is 7.

21 21 Checking if s =‘asdfdsa’ is a palindrome. len(s) = 7 jS[j]k =len-1-jS[k] 0‘a’6

22 22 Checking if s =‘asdfdsa’ is a palindrome. len(s) = 7 jS[j]k =len-1-jS[k] 0‘a’6 1‘s’5

23 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 24 Write a function that has a 3-digit int parameter, returns the digit that is the middle one according to size.

25 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 26 substrings A substring is a part of a string upto and including the string itself.

27 27 If s = ‘asdfg’, to get the entire string, use s[0:len(s)].

28 28 Here len(s) is 5, so s[0:5] gives you the entire string. Note that the 2 nd parameter (here 5) is one more than the index of the last character in the desired string character‘a’‘s’‘d’‘f’‘g’ index01234

29 29 What would s[1:4] give?

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

31 31 S[2:4] → ‘sdf’ What is S[2:2]?

32 32 What is S[2:2]? It’s the empty string since the string character indices go from 2 to 1.

33 33 You can number the characters from the back of the string starting with -1 as is shown next

34 34 character‘a’‘s’‘d’‘f’‘g’ index-5-4-3-2

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

36 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 37 def reverse(s):#prints a string in reverse n = len(s) for j in range(-1, -(n+1), -1): print(s[j])

38 38 Write a program that produces a triangle.

39 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 40 The entire 8-character string is represented by s[0:9] How do we write the range in the for loop?

41 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 42 def triangle(s):#produces a triangle n = len(s) for j in range(1, n+1): print(s[0:j])

43 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 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].


Download ppt "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."

Similar presentations


Ads by Google