![Page 1: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/1.jpg)
1
String processing
Samuel Marteck ©2010
![Page 2: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/2.jpg)
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’ ?
![Page 3: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/3.jpg)
3
character ‘a’ ‘c’ ‘4’ ‘5’ ‘3’
index 0 1 2 3 4
![Page 4: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/4.jpg)
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?
![Page 5: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/5.jpg)
5
character ‘a’ ‘c’ ‘4’ ‘5’ ‘3’
index 0 1 2 3 4
![Page 6: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/6.jpg)
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
![Page 7: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/7.jpg)
7
The following prints the characters in a string:
![Page 8: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/8.jpg)
8
s = 'abcd'
length = len(s)
for j in range(length): # 0 <= j <= length -1 print(s[j]) #prints each character in s
![Page 9: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/9.jpg)
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.
![Page 10: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/10.jpg)
10
For s = 'abcd‘, the length is 4. What would
s[4] produce?
![Page 11: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/11.jpg)
11
For s = 'abcd‘, the length is 4. What would
s[4] produce?
It produces an error message:
IndexError: string index out of range
![Page 12: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/12.jpg)
12
In s = 'abcd‘, can you replace the ‘c’ with a
‘z’ by writing s[2] = ‘z’ ?
![Page 13: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/13.jpg)
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’?
![Page 14: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/14.jpg)
14
Let’s write a function to determine if every
letter in a word is lowercase.
![Page 15: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/15.jpg)
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?
![Page 16: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/16.jpg)
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.
![Page 17: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/17.jpg)
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
![Page 18: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/18.jpg)
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.
![Page 19: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/19.jpg)
19
Checking if ‘asdfdsa’ is a palindrome. Is it
symmetric about the middle character?
![Page 20: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/20.jpg)
20
Checking if ‘asdfdsa’ is a palindrome. Its
length is 7.
![Page 21: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/21.jpg)
21
Checking if s =‘asdfdsa’
is a palindrome. len(s) =
7
j S[j] k =len-1-j S[k]
0 ‘a’ 6 ‘a’
![Page 22: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/22.jpg)
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’
![Page 23: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/23.jpg)
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()
![Page 24: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/24.jpg)
24
Write a function that has a 3-digit int
parameter, returns the digit that is the
middle one according to size.
![Page 25: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/25.jpg)
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
![Page 26: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/26.jpg)
26
substrings
A substring is a part of a string upto and
including the string itself.
![Page 27: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/27.jpg)
27
If s = ‘asdfg’, to get the entire string, use
s[0:len(s)].
![Page 28: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/28.jpg)
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
![Page 29: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/29.jpg)
29
What would s[1:4] give?
![Page 30: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/30.jpg)
30
character ‘a’ ‘s’ ‘d’ ‘f’ ‘g’
index 0 1 2 3 4
![Page 31: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/31.jpg)
31
S[2:4] → ‘sdf’
What is S[2:2]?
![Page 32: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/32.jpg)
32
What is S[2:2]?
It’s the empty string since the string
character indices go from 2 to 1.
![Page 33: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/33.jpg)
33
You can number the characters from the
back of the string starting with -1 as is
shown next
![Page 34: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/34.jpg)
34
character ‘a’ ‘s’ ‘d’ ‘f’ ‘g’
index -5 -4 -3 -2 -1
![Page 35: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/35.jpg)
35
• How would you write a function to print a string in reverse?
![Page 36: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/36.jpg)
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.
![Page 37: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/37.jpg)
37
• def reverse(s):#prints a string in reverse
• n = len(s)
• for j in range(-1, -(n+1), -1):
• print(s[j])
![Page 38: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/38.jpg)
38
Write a program that produces a triangle.
![Page 39: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/39.jpg)
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?
![Page 40: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/40.jpg)
40
The entire 8-character string is represented
by s[0:9]
How do we write the range in the for loop?
![Page 41: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/41.jpg)
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?
![Page 42: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/42.jpg)
42
• def triangle(s):#produces a triangle
• n = len(s)
• for j in range(1, n+1):
• print(s[0:j])
![Page 43: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/43.jpg)
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)].
![Page 44: 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](https://reader030.vdocuments.mx/reader030/viewer/2022032521/56649d585503460f94a3759c/html5/thumbnails/44.jpg)
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].