Python字符类型

来源:互联网 发布:特效拍摄软件下载 编辑:程序博客网 时间:2024/06/05 02:15

字符类型:

s = “hello” ==> hellos[0]  ==>h   //数组s[-1] ==>o     //python独有,负数表示倒取字符串s[1:4]   ==>ell    //从s[1]开始取值,到s[4]前结束(不含s[4])s[1:-1]  ==>ell    //从s[1]开始取值,到s[-1]结束s[1:]   ==>ello   //从s[1]开始取,直到结束s[:2]   ==>he    //从s[0]到s[2]之前结束(不含s[2])
str = ’0123456789′print str[0:3] #截取第一位到第三位的字符print str[:] #截取字符串的全部字符print str[6:] #截取第七个字符到结尾print str[:-3] #截取从头开始到倒数第三个字符之前print str[2] #截取第三个字符print str[-1] #截取倒数第一个字符print str[::-1] #创造一个与原字符串顺序相反的字符串   怎么理解print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符print str[-3:] #截取倒数第三位到结尾print str[:-5:-3] #逆序截取,具体啥意思没搞明白? 等同于:print str[-1:-5:-1]s = "123456789"print str[0:10:2]  2为步长print str[4:1:-1] 当反着取得时候需要填-1,表示第一个大、第二个小

字符类型内置函数:

find() 返回数组内从左取第一个被查询值的坐标(索引功能)
rfind() 返回数组内从右取第一个被查询值得坐标
python中find的函数的功能是查找指定的字符串并返回该字符串的起始位置。
函数原型:find(str, pos_start, pos_end)
参数如下:
str:被查找“字符串”
pos_start:查找的首字母位置(从0开始计数。默认:0)
pos_end: 查找的末尾位置(默认-1)
返回值:如果查到:返回查找的第一个出现的位置。否则,返回-1。

s.find("e")   ==> 1     s.find("l")   ==>2      //取得是第一个“l”,字符串也一样
s2 = "Python's own help system"s2.find(" ")   ==>8  //返回空格的坐标s2[9:]    ==own help system    //返回s[9]直到结束的字符串

count(substr, [start, [end]]) #计算substr在S中出现的次数
replace(oldstr, newstr, [count]) #把S中的oldstar替换为newstr,count为替换次数。这是替换的通用形式,还有一些函数进行特殊字符的替换

In [42]: s = "aaaaaaaaaaaaaaaaaaaaaaaaaaa"Out[42]: 'aaaaaaaaaaaaaaaaaaaaaaaaaaa'In [43]: s.replace('aa','b',2).count('a')Out[43]: 23

粘贴语法 %paste

for循环 #注意语法格式,循环体内的语句必须有缩进,且必须缩进数量一致

下面 Case1和Case 中count的实际作用是不一样的
Case1:count = 0for a in s:     print a    count +=1

Case2: 比对Case1,没有对齐的情况

count = 0for a in s:    pirnt acount +=1

for 嵌套循环 //注意格式的不同

for a in s:     print a     for b in s:          print b

切割函数split(”字符”)
如果目标字符串里有双引号,外面用单引号,或相反

s = "hello world!"In [38]: s.split('l')Out[38]: ['he', '', 'o wor', 'd!']   #注意第二个“l”也被劈开,但用空来代替

decode方法,字符转换
方法:s.decode(“utf-8”)
utf-8和GBK的却别:utf-8 中文3个字节 英文1个字节 各个语言有单独的code在里面,不需单独语言包
GBK是 国标 汉字两个字节,而且只支持中文简体,GBK包含GB2312

s = "你"In [39]: %pastes2 = "你"In [40]: s2 Out[40]: '\xe4\xbd\xa0'    #此处输出的是16进制编码print s.decode("utf-8").encode("gbk")
       如果是printf 会自动转Unicode,如果是字符串比较就得转了

小练习:
将字符串 s = ‘贴吧’
中的“贴吧”提取出来
方法1:利用find方法,
方法2:使用split切割函数分割
print s.split(“>”)[1].split(“<”)[0].decode(“utf-8”)

—————————————-下午————————————–

strip() //删除两端的指定字符,括号里面的东西本身部分先后顺序
去两边空格和回车:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
去两边字符串:str.strip(‘d’),相应的也有lstrip,rstrip

str=' python String function 'print '%s strip=%s' % (str,str.strip())str='python String function'print '%s strip=%s' % (str,str.strip('d'))

小练习:删除字符串d中的空格

d = "config = 5"result = ""for a in d.split(' '): result += aprint result

len()获取字符串长度
字符串的测试函数,这一类函数在string模块中没有,这些函数返回的都是bool值:

S.startwith(prefix[,start[,end]]) #是否以prefix开头 S.endwith(suffix[,start[,end]])  #以suffix结尾 S.isalnum()  #是否全是字母s和数字,并至少有一个字符 S.isalpha()  #是否全是字母,并至少有一个字符 S.isdigit()  #是否全是数字,并至少有一个字符 S.isspace() #是否全是空白字符,并至少有一个字符 S.islower() #S中的字母是否全是小写 S.isupper() #S中的字母是否便是大写 S.istitle() #S是否是首字母大写的

小练习
提取字符串 s = “2afdsag123dsaba123” 中的所有数字,结果”2 123 123”

s = "2afdsag123dsaba123"s1 = ""t = Falsefor a in s:    if a.isdigit():    s1 +=a    t = True    if not a.isdigit() and t == True:      t = False      s1 += " "print s1 

转义字符
s1 = r”abc\n” r是raw原始数据
s2 = u”abc\n” 同s2 = “abc\n” u是unicode

if分支判断:

s = 2if s == 1:    print "A"elif s == 2:    print "B"else:    print "C"

in包含关系
s = “hello”
“e” in s
True //输出结果

小练习:判断当前输入的一个字符串是否是手机号

set = "13,18,17,15"    //以什么开头a =raw_input("Please input:")if len(a) == 11 and a.isdigit() and a[:2] in set1:    print "Success!"else:    print "error!"

小练习2:删除字符串中s中的左右空格,类似实现strip的功能

s="    12  35a "start = 0end = 0for a in s:    if a== ''    start += 1else  breakfor a in s[::-1]:    if a== ''        end += 1    else        breakprint s[start:len(s)- end]
0 0
原创粉丝点击