常见的几个Python面试题

 1 str = "abcdaab"
 2 #字符串反转
 3 print str[::-1]
 4 
 5 #统计str字符串中出现'a'的次数
 6 print str.count('a')
 7 
 8 #pyhon可以用+作为字符串连接符号
 9 print str + "efg"
10 
11 def sum(n):
12     result = 0
13     i = 1
14     while i <= n:
15         result += i;
16         i += 1
17     return result
18 
19 print sum(10)
20 
21 def multi(n):
22     result = 1
23     i = 1
24     while  i <= n:
25         result *= i
26         i += 1
27     return result
28 
29 print multi(5)
30 
31 #查找'b'所在的位置,默认从下标0开始查找
32 print str.find('b')
33 #查找'b'所在的位置,从下标1/第四个字符开始查找
34 print str.find('b',3)
35 #查找'xyz',找不到返回'-1'
36 print str.find('xyz')
37 print str.index('a')
原文地址:https://www.cnblogs.com/dtest/p/4530981.html