Python基础学习之字符串

向来字符串的方法和功能比较强大:

>>> str="use python do intersting thing"
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

下面就分类介绍一下

1. 字符串的截取操作或者可以成为切片
字符串的index

>>> str[0]
'u'
>>> str[1]
's'
>>> str[-1]
'g'
>>> str[1:8]
'se pyth'
>>> str[1:10:2]
's yhn'
>>> str[1:10:3]
'sph'
>>> str[1:]
'se python do intersting thing'
>>> str[:-1]
'use python do intersting thin'
>>> str[:]
'use python do intersting thing'
>>> str[29]
'g'
>>> str[-29]
's'
>>> str[-30]
'u'
>>> str[-1]
'g'
>>> str[29]
'g'
>>> str[-3:]
'ing'
>>> str[-3]
'i'
>>> str[-3:-1]
'in'
>>> str[-1]
'g'
>>> str[1:3]
'se'
>>> str[1]
's'
>>> str[3]
' '
>>> str[0:2]
'us'
>>> str[0]
'u'
>>> str[2]
'e'
>>> str[:-3:-2]
'g'
>>> str[:-3:]
'use python do intersting th'
>>> str[:-3:2]
'uepto oitrtn h'
>>> str[27:29:2]
'i'
>>> str[27]
'i'
>>> str[27::2]
'ig'
>>> 
>>> str[0:5:-5]
''
>>> str[0:5:-2]
''
>>> str[10:20:-2]
''
>>> str[-10:-20:-2]
'trtio'
>>> str[:-20:-2]
'gitgisen d'
>>> str[:-20:2]
'uepto'
>>> str[10:20:-2]
''
>>> str[-10:-20:2]
''
>>> 

str[startIndex:endIndex:step]
字符串截取的总结:字符串的截取总是遵循前闭后开的原则“[ )”截取的部分总是包括最开始的部分str[startIndex],不包括str[endIndex],step为截取的跳跃步长,在Python中特殊的地方是可以用负数表示Index,即原来是[1,2,3….],现在也可以是[…-3,-2,-1],如果步长为负数则表示截取的方向与startIndex–>endIndex的方向相反及endIndex–>startIndex,步长的正负只代表截取的方向,在有些情况下,并不适用。

2.字符串的字母大小写处理

#获取字符串的长度
>>> len(str)
30
#将字符串转换成小写
>>> str.lower()
'use python do intersting thing'
#将字符串转换大小写
>>> str.swapcase()
'USE PYTHON DO INTERSTING THING'
#将字符串转换成大写
>>> str.upper()
'USE PYTHON DO INTERSTING THING'
#将字符串的首字母转换成大写
>>> str.capitalize()
'Use python do intersting thing'
#将字符串的首字母全部转换成大写
>>> str.title()
'Use Python Do Intersting Thing'
>>> 

3.字符串的左对齐,右对齐,居中对齐等格式化

#获取固定长度,右对齐,左边不够用空格补齐
>>> str.ljust(50)
'use python do intersting thing  
#获取固定长度,左对齐,右边不够用空格补齐                  '
>>> str.rjust(50)
'                    use python do intersting thing'
#获取固定长度,中间对齐,两边不够用空格补齐
>>> str.center(50)
'          use python do intersting thing          '
#获取固定长度,右对齐,左边不足用0补齐
>>> str.zfill(50)
'00000000000000000000use python do intersting thing'
>>> 

4.字符串的查找

#查找匹配关键字母所在位置的第一次出现时对应的index
>>> str.find('i')
14
#从某个index开始,匹配查找
>>> str.find('i',15)
21
#从某个index到某个index,匹配查找
>>> str.find('i',22,29)
27
#从右边开始查找
>>> str.rfind('i')
27
#找不到对应的匹配,返回-1
>>> str.find('z')
-1
>>> str.index('i')
14
>>> str.index('i',15)
21
>>> str.index('i',22,29)
27
>>> str.rindex('i')
27
>>> str.rindex('z')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> 

find的都可以用index代替只不过如果找不到,index会抛出异常

5.字符串的连接,去空格,替换

#字符串的拼接
>>> "let us " +str
'let us use python do intersting thing'
#字符串的相乘
>>> str*2
'use python do intersting thinguse python do intersting thing'
#数组按照特定的字符拼接成字符串
>>> "-".join(str.split(' '))
'use-python-do-intersting-thing'
>>> str.split(' ')
#将字符串按照特定的字符串分离转换成数组
['use', 'python', 'do', 'intersting', 'thing']
>>> "-".join(str)
#将字符串有特定的字符连接
'u-s-e- -p-y-t-h-o-n- -d-o- -i-n-t-e-r-s-t-i-n-g- -t-h-i-n-g'
#str.replace(x,y) 用y代替x 全部替换
>>> str.replace('python','java')
'use java do intersting thing'
#str.replace(x,y,num) 用y代替x 限制替换的次数为num
>>> str.replace('i','I',2)
'use python do InterstIng thing'
>>> str.rjust(50)
'                    use python do intersting thing'
#去除字符串左边的空格
>>> str.rjust(50).lstrip()
'use python do intersting thing'
>>> str.ljust(50)
'use python do intersting thing                    '
#去除字符串右边的空格
>>> str.ljust(50).rstrip()
'use python do intersting thing'
>>> str.center(50)
'          use python do intersting thing          '
>>> str.center(50).strip()
#去除字符串两边的空格
'use python do intersting thing'
#去除字符串两边特定的字符相应的也有lstrip,rstrip
>>> str.strip('g')
'use python do intersting thin'
>>> 

6.字符串的判断

#是否以某个字符开头
>>> str.endswith('use')
False
#是否以某个字符结尾
>>> str.startswith('use')
True
#是否全是字母或者数字
>>> str.isalnum()
False
#是否全是字母
>>> str.isalpha()
False
#是否全是数字
>>> str.isdigit()
False
#是否全是小写
>>> str.islower()
True
#是否全是大写
>>> str.isupper()
False
>>> 

7.打印格式

>>> print "%s like %s" %('we','python')
we like python

8.字符串的转译

>>> print 'c:\now start'
c:
ow start
>>> print r'c:\now start'
c:\now start
>>> print 'c:\\now start'
c:\now start
>>> 

字符串的转译可以采用: r’ 字符串’ ,r的意思是保持字符串的原有形式,或者对应特殊符号加斜杠进行转译
字符串单引号(‘‘),双引号(““),三引号(“” _”)的区别:
如果一个字符串中已经有单引号,则使用双引号,如果既有单引号又有双引号,使用三引号。

在使用中文的时候,在python的代码中要加上注释

# coding: utf-8
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值