-
Notifications
You must be signed in to change notification settings - Fork 0
/
py4e_6_str.py
47 lines (46 loc) · 923 Bytes
/
py4e_6_str.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python3
''' Python for everybody
chapter 6 '''
#With while loop
string = input('Enter a string:')
index = 0
while index < len(string):
letter = string[index]
print(index, letter)
index += 1
#With for loop
string = input('same with for loop:')
index = 0
for i in string:
print(index, i)
index += 1
#Count a letter
count = 0
for i in string:
if i == "n":
count +=1
print(count)
# while loop works its way backwards
print(string, len(string))
count = -1
while count >= -6:
print(count, string[count])
count -=1
#for loop works its way backwards
print('Same with for')
index = -1
for i in string:
print(index, string[index])
index -=1
#Chack's example
data = 'From [email protected] Sat Jan 5 09:14:16 2008'
atpos = data.find('@')
print(atpos)
21
sppos = data.find(' ',atpos)
print(sppos)
31
host = data[atpos+1:sppos]
print(host)
uct.ac.za
>>>