python 字符串的特性

#######str字符串#####
str字符判断大小写
   url1 = 'http://www.cctv.com'
   url2 = 'file:///mnt'
   print url1.startswith('http')   #找出字符是否以'...'开头
   print url2.startswith('file')   #找出字符串是否以'...'开头
   print '123'.isdigit()  #判断是否为数字
   print '1a1'.isdigit()   #判断是否为数字
   print 'Hello'.istitle()  #判断是否位标题,第一个字母大写,其余小写
   print 'hello'.upper()  #将小写转成大写
   print 'HELLO'.lower()  #将大写转换成小写
   print 'hello'.islower()  #判断是小写
   print 'HELLoO'.isupper() #判断是大写
str字符串开头和结尾匹配
 s = 'hello.jpg'
   print s.endswith('.jpg')#找出字符串是否以...结尾
str字符串的分离和连接
分离
 s = '172.25.254.250'
   sl = s.split('.')   #split对于字符串进行分离,分割符为'.'
连接
   print '/'.join('hello')  #连接符为‘/’
   h/e/l/l/o
str字符串的特性
# 索引:0,1,2,3,4 索引值是从0开始
   s = 'hello'       #定义字符串
   print s[0]        #索引第0个字符,即“h”
   print s[1]        #索引第一个字符,即“e”
# 切片
# 切片的规则:s[start:end:step] 从start开始到end-1结束,步长:step
   print s[0:3]
   print s[0:4:2]
   print s[:]       # 显示所有字符
   print s[:3]        # 显示前3个字符
   print s[::-1]      # 对字符串倒叙输出
   print s[1:]      # 除了第一个字符以外,其他全部显示
   print s * 10      # 重复10次
   print 'hello ' + 'world'   # 连接
# 成员操作符
   print 'q' in s     # 查找字符串中是否有‘q’,有则为“True”,没有则为“False”
str字符串的搜索和替换
 s = 'hello world'
   print len(*)            #统计字符串中字符个数
   print  s.find('world')         #find 找到字符串,并返回最小索引
   print s.replace('hello','westos')  #替换所指定的字符串
str字符串的统计
   print ‘heelloo’.count('o')          #统计字符串中有几个‘o’
 
原文地址:https://www.cnblogs.com/zhengyipengyou/p/9573445.html