#coding:utf-8
#两个小函数
#一、查找字符在字符串中第一次出现的位置.
def find(string, char):
index = 0
while index < len(string):
if (string[index] == char):
return index
index += 1
return -1
#二、查找字符在字符串中的总数
def findSum(string, char):
index = 0
count = 0
while index < len(string):
if (string[index] == char):
count += 1
index += 1
return count
#使用以上两个函数
print "字符1在字符串1211211234中第一次出现的位置: ", find("1211211234", "1")
print "字符1在字符串1211211234中出现的次数:", findSum("1211211234", "1")
import string #引入string库
print string.find('www.cctv.com', 'com') #result=9
print string.find('Good','d') #result = 3
print string.find('canada', 'a',2,9) #result =3,用法如下:
#string.find(s, sub[, start[, end]])函数说明
#Return the lowest index in s where the substring sub is found such that sub is
#wholly contained in s[start:end]. Return -1 on failure. Defaults for start and
#end and interpretation of negative values is the same as for slices.
print string.lowercase #常量,abcdefghijklmnopqrstuvwxyz
print string.uppercase #常量,ABCDEFGHIJKLMNOPQRSTUVWXYZ
print string.digits #常量,0123456789
def isLower(char): #判断一个字符是否为小写
if (string.find(string.lowercase, char) > -1):
return 'Good'
return 'bad'
print isLower('S')
def isLowertest(char): #另外一种判断字符是否为小写的方法
return char in string.lowercase
print isLowertest('a')