Python字符串在使用的时候是需要我们进行详细的学习。下面我们就看看在具体的操作中如何才能更好的进行这个字符串的使用方法。在之后的使用中还是需要我们继续学习。
#coding:utf-8
#两个小函数
#一、查找字符在字符串中第一次出现的位置.
def find(string, char):
index = 0
while index < len(string):
if (string[index] == char):
return index
index += 11
return -111
#二、查找字符在字符串中的总数
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("1211
211234", "1")
print "字符1在字符串1211211234中出现的次数:", findSum("1211211
234", "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 #常量,012345678937
def isLower(char): #判断一个字符是否为小写
if (string.find(string.lowercase, char) > -1):
return 'Good'
return 'bad'
print isLower('S')
def isLowertest(char): #另外一种判断字符是否为小写的方法
return char in string.lowercase46 print isLowertest('a')
- 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.
以上就是对Python字符串的相关介绍,希望大家有所收获。
【编辑推荐】