Python基本类型-字符串

字符串记录文本信息,从严格意义上说字符串是单个字符从左到右顺序排列的有序集合。我们可以通过列表的方法获取字符串长度或对应下标的字符

1 >>>s="hello python"
2 >>>len(s)
3 12
4 >>>s[2]
5 'l'
6 >>>s[7]
7 'y'
View Code

有趣的是在Python中我们可以反向索引,这个特点给我们省了不少事

>>>s[-1]
'n'
>>>s[-2]
'o'
View Code

一般来说负的索引会简单的跟字符串长度相加,因此一下两个操作是等效的

>>>s[-1]
'n'
>>>s[len(s)-1]
'n'
View Code

除了可以简单的从位置索引,序列也支持一种所谓分片的操作,在一个分片中,左边界默认为0,右边界默认为分片序列的长度

>>>s[1:3]
'el'
>>>s[1:]
'ello python'
>>>s[:3]
'hel'
View Code

当然,我们也可以通过负索引来分片

>>>s[:-2]
'hello pyth'
View Code

作为一个序列,字符串也支持通过加号进行合并或者通过乘号重复

>>>s+" my love"
'hello python my love'

有趣的是在Python中字符串可以直接与数字相乘,结果是什么样的呢

>>>"test!"*3
'test!test!test!'

会得到一个被乘字符串重复多次后拼接的新字符串

值得一提的是字符串存在不可变性,当我们要改变字符串中的某个字符时,程序就会抛出异常

>>>s[0]='T'
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: 'str' object does not support item assignment

不过我们可以通过给变量重新赋值的方式达到同样的效果

>>>s='T'+s[1:]
>>>s
'Tello python'

在Python核心类型中,数字,字符串,元组都具有不可变性

以上介绍的都是字符串操作都是一个真正的序列操作,也就是说这些操作在其他序列中也会工作,包括列表和元组。接下来介绍一个字符串独有的一些操作。

>>>s.find('py')             #查找子字符串下标
6

>>>s.replace('o','t')      #全局替换字符或子字符串
'hellt pythtn'

>>>test='aaa,bbb,ccc,ddd'   #按照指定字符串分割,返回分割后的字符串列表
>>>test.split(',')
['aaa', 'bbb', 'ccc', 'ddd']

>>>s.upper()                #将所有字母转为大写
'HELLO PYTHON'

>>>s.isalpha()              #判断字符串是否是纯字母
False

>>>s.isdigit()                #判断字符串是否是纯数字

具体方法就不一一介绍了,如果想了解字符串都有哪些方法可以执行帮助方法dir(),传入参数就是想要了解的变量的方法,如果想知道某个方法具体的用法,可以执行help方法。具体操作如下:

>>>dir(s)
['__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']
>>> help(s.replace)
Help on built-in function replace:

replace(...)
    S.replace(old, new[, count]) -> string
    
    Return a copy of string S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.

 

转载于:https://www.cnblogs.com/On-my-way/p/6813217.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值