-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path6-12.py
More file actions
52 lines (41 loc) · 1.03 KB
/
Copy path6-12.py
File metadata and controls
52 lines (41 loc) · 1.03 KB
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
48
49
50
51
52
#! /usr/bin/env python
# encoding:utf-8
import types
#(a)
def findchr(string,char):
print "the string is %s,the char is %s" %(string,char)
result = []
for i ,j in enumerate(string):
if char == j:
result.append(i)
if len(result) != 0:
print "the index of char:"
return result
else:
return -1
print (findchr("yesterday once more",'o'))
#(b)
def rfindchr(string,char):
print "the string is %s,the char is %s" %(string,char)
l = len(string)
for i,j in enumerate(string[::-1]):
if char == j:
result = l-i
break
if type(result) is types.IntType:
print "the last index of char:"
return result
else:
return -1
print (rfindchr("yesterday once more",'o'))
#(c)
def subchr(string,origchar,newchar):
print "the string is %s,the origchar is %s,the newchar is %s" %(string,origchar,newchar)
result = []
str_list = list(string)
for i,j in enumerate(str_list):
if origchar == j:
str_list[i]= newchar
result = ''.join(str_list)
return result
print (subchr("yesterday once more","once more","is beautiful"))