2018-06-26 python字符串的一些总结

最近看完了python字符串的内容,总结如下:

  1. 切片使用
>>> astr="i am zhaoxin"
>>> astr[1:]  ##取除第一个元素外
' am zhaoxin'
>>> astr[:-1]  ##取除最后个元素外
'i am zhaoxi'
>>> astr[2:4]  ##取中间am
'am'
>>> astr[:]  ## 完全复制字符串
'i am zhaoxin'
>>> astr.split(" ")  ##split分隔符使用,返回列表形式
['i', 'am', 'zhaoxin']
  1. 替换函数,由于字符串不可变,则替换实际是生成新的字符串
>>> astr.replace(" ","_")
'i_am_zhaoxin'
>>> astr
'i am zhaoxin'
  1. 计算字符串长度
>>> len(astr)
12
  1. 查找一个元素的索引位置,只返回第一次查找的位置
>>> astr.index("a")
2
  1. 查找元素或者子字符串是否在父字符串中
## find查找
str.find(astr, beg=0, end=len(string))  ## str 父字符串 astr子字符串 beg 查找索引开始地方,默认为0 end 索引结束地方,默认字符串长度

>>> astr.find("a")  ##返回父字符串索引开始的位置
2
>>> astr.find("songzhen")  ## 没有则放回-1
-1
>>> astr.find("a",4)  ## 索引开始为4的地方开始
7

## startswith查找 检查字符串是否以astr开始
str.startswith(astr, beg=0,end=len(string))  ## str 父字符串 astr子字符串 beg 查找索引开始地方,默认为0 end 索引结束地方,默认字符串长度

>>> astr.startswith("a")
False
>>> astr.startswith("i")
True
>>> astr.startswith("a",3)
False
>>> astr.startswith("a",2)
True

## endswith查找 查找字符串是否以astr为后缀
str.endswith(astr, beg=0,end=len(string))  ## str 父字符串 astr子字符串 beg 查找索引开始地方,默认为0 end 索引结束地方,默认字符串长度

>>> astr.endswith("zhaoxin")
True
>>> astr.endswith("am")
False
>>> astr.endswith("am",0,len(astr)-len("zhaoxin")-1)
True

## in 指令 

>>> "a" in astr
True
>>> "zhaoxin" in astr
True
>>> "zhaoxinaa" in astr
False
  1. 字符串格式化
##  % 格式化·
>>> "%s and %s and %s" %("zhaoxin","songzhen","xiaodi")
'zhaoxin and songzhen and xiaodi'
>>> "%(one)s and %(two)s and %(three)s" % {"one":"zhaoxin","two":"songzhen","three":"xiaodi"}
'zhaoxin and songzhen and xiaodi'

## str.format
>>> "{0} and {1} and {2}".format("zhaoxin","songzhen","xiaodi")
'zhaoxin and songzhen and xiaodi'
>>> "{one} and {two} and {three}".format(one="zhaoxin",two="songzhen",three="xiaodi")
'zhaoxin and songzhen and xiaodi'

## 格式化对齐
>>> "{one:10} and {two:10} and {three:10}".format(one="zhaoxin",two="songzhen",three="xiaodi") 
'zhaoxin    and songzhen   and xiaodi    '
>>> "{one:<10} and {two:<10} and {three:<10}".format(one="zhaoxin",two="songzhen",three="xiaodi")
'zhaoxin    and songzhen   and xiaodi    '
>>> "{one:>10} and {two:>10} and {three:>10}".format(one="zhaoxin",two="songzhen",three="xiaodi")
'   zhaoxin and   songzhen and     xiaodi'
  1. 字符串删除操作
## str.strip([chars])  删除字符串头尾指定元素,默认为空格
>>> str="123aaaa321"
>>> str.strip("12")
'3aaaa3'

## str.rstrip([chars]) 删除字符串末尾指定元素,默认为空格
>>> str.rstrip("12")
'123aaaa3'

## str.lstrip([chars]) 删除字符串头部指定元素,默认为空格
>>> str.lstrip("12")
'3aaaa321'

##删除指定索引元素
>>> a="123"
>>> a[:1]+a[2:]
'13'
  1. 字符串拼接
## +运算法则
>>> a="222"
>>> b="dess3"
>>> a+b
'222dess3'

## ,   注意中间会默认加一个空格
>>> print a,b
222 dess3
## * 运算符 即复制
>>> a*4
'222222222222'
>>> b*4
'dess3dess3dess3dess3'

8.字符串比较

## ==  操作
>>> a="123"
>>> b="123"
>>> c="124"
>>> a==b
True
>>> a==c
False
>>> b==c
False

## is 运算符
>>> a is b
True
>>> a is c
False
>>> b is c
False

##  cmp(x, y) -> integer x=y 返回0, x<y 返回-1,  x>y 返回1
>>> cmp(a,b)
0
>>> cmp(a,c)
-1
>>> cmp(c,b)
1
  1. 字符串一些类型判断
## str.isalnum() 判断所有字符都是数字或者字母
>>> a="123"
>>> b="abc"
>>> a.isalnum()
True
>>> b.isalnum()
True
>>> c="12sdc3es"
>>> c.isalnum()
True

## str.isalpha() 判断所有字符都是字母
>>> a.isalpha()
False
>>> b.isalpha()
True
>>> c.isalpha()
False

## str.isdigit() 判断所有字符都是数字
>>> a.isdigit()
True
>>> b.isdigit()
False
>>> c.isdigit()
False

## str.islower() 判断所有字符都是小写
>>> d="abc"
>>> e="ABC"
>>> f="aBc"
>>> d.islower()
True
>>> e.islower()
False
>>> f.islower()
False

## str.isupper() 判断所有字符都是大写
>>> d.isupper()
False
>>> e.isupper()
True
>>> f.isupper()
False

## str.istitle() 判断所有单词都是首字母大写,像标题
>>> g="Tile Is Not true"
>>> h="Tile Is Not True"
>>> i="Tile Is NoT True"
>>> g.istitle()
False
>>> h.istitle()
True
>>> i.istitle()
False

## str.isspace() 判断所有字符都是空白字符、\t、\n、\r
>>> j="  \n\n  \t \r   "
>>> k="  \nssss\n  \t \r   "
>>> j.isspace()
True
>>> k.isspace()
False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值