Python学习笔记八:字符串的操作(二)
2010-03-11 08:25
911 查看
#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')
相关文章推荐
- 学习笔记之Python 切片:利用切片操作,实现一个trim()函数,去除字符串首尾的空格
- Python学习笔记七:字符串的操作(一)
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- python学习笔记——字符串及其主要的操作
- python学习笔记:字符串操作大全
- Python学习笔记5-字符串、bool、数值操作和数组字典排序
- Python学习笔记5-字符串、bool、数值操作和数组字典排序
- Python学习笔记5-字符串、bool、数值操作和数组字典排序
- Python学习笔记:字符串基本操作
- Python学习笔记(二):基本数据类型及操作(逻辑、字符串、浮点、复数)
- python学习笔记之二:字符串操作
- Python学习笔记--字符串操作string
- Python学习笔记二(String字符串操作)
- python学习笔记1(字符串操作)
- Python学习笔记(23)-Python字符串常用操作
- Python学习笔记七:字符串的操作(一)
- Python学习笔记5-字符串、bool、数值操作和数组字典排序
- python学习006--字符串操作
- Python tutorial: python中文快速学习笔记 - 数字与字符串