Python之字符串对象

字符串对象

首先我们要知道什么是字符串呢?
字符串或串(String)是由数字、字母、下划线组成的一串字符
字符串在存储上类似字符数组,所以它每一位的单个元素都是可以提取的,字符串也可作为容器。

现在知道了字符串,那么如何定义它呢?
很简单,用单引号’ ’ 双引号"" 或者 三引号""" “”" 引起来

下面我们来看看字符串的常用方法:

1.capitalize ——将字符串的首字母大写

>>> str = "my favorite star is xiaozhan"
>>> str.capitalize()
'My favorite star is xiaozhan'
字符串首字母m大写了

2.center——将字符串居中

center(width, fillchar=' ', /)
第一个参数是尺寸;
第二个参数是填充符号,默认填充'',填充符号可变
>>> str.center(50)
'           my favorite star is xiaozhan           '
>>> str.center(50,"#")
'###########my favorite star is xiaozhan###########'

3.count——统计字符串中出现的字符或者字符串个数

>>> str.count("a")
4
>>> str.count("is")
1

4.encode——将字符串转换为字节
编码方式有:gbk、gb2312、ANSI、unicode(utf-8)
建议大家在进行编码转换时使用utf-8

>>> str.encode()
b'my favorite star is xiaozhan'

英文编码转换我们看不出效果,那么再来看一看中文的

>>> ls = "我最喜欢的明星是肖战"
>>> ll = ls.encode("utf-8")
>>> ll
b'\xe6\x88\x91\xe6\x9c\x80\xe5\x96\x9c\xe6\xac\xa2\xe7\x9a\x84\xe6\x98\x8e\xe6\x98\x9f\xe6\x98\xaf\xe8\x82\x96\xe6\x88\x98'

注意:编码和解码(decode)一定使用同一种标准!!!
编码时使用的是utf-8,解码时也要用utf-8,使用其他就会报错
解码用的是字节的decode方法

>>> ll.decode("utf-8")
'我最喜欢的明星是肖战'
>>> ll.decode("gbk")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 10: illegal multibyte sequence

5.endswith——判断字符是否以xx结尾

>>> str = "my favorite star is xiaozhan"
>>> str.endswith("y")
False
>>> str.endswith("n")
True

6.startswith——判断字符串是否已xxx开头

>>> str.startswith("g")
False
>>> str.startswith("m")
True

7.find——查找字符串中某个字符或者字符串第一次出现的位置,注意:如果不存在,则返回-1

>>> str.find("a")
4
>>> str.find("b")
-1

8.rfind——查找字符串中某个字符或者字符串最后一次出现的位置,注意:如果不存在,则返回-1

方法名前加 “r”,表示相反

>>> str.rfind("a")
26
>>> str.rfind("b")
-1

9.index——查找字符串中某个字符或者字符串第一次出现的位置,注意:如果不存在,则抛出错误

>>> str.index("a")
4
>>> str.index("b")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

10.rindex——查找字符串中某个字符或者字符串最后一次出现的位置,注意:如果不存在,则抛出错误

>>> str.rindex("a")
26
>>> str.rindex("b")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

11.format——Python3推出的一种新的格式化字符串的方式
{ }相当于%

>>> a=10
>>> b=20
>>> print("{}+{}={}".format(a,b,a+b))
10+20=30

12.join——用来拼接字符串,注意参数是一个可迭代对象

>>> str = 'my favorite star is xiaozhan'
>>> >>> "*".join(str)
'm*y* *f*a*v*o*r*i*t*e* *s*t*a*r* *i*s* *x*i*a*o*z*h*a*n'

13.split——按照特定规则分割字符串,默认按照空格分隔

>>> str = 'my favorite star is xiaozhan'
>>> str.split()
['my', 'favorite', 'star', 'is', 'xiaozhan']
>>> str.split("t")
['my favori', 'e s', 'ar is xiaozhan']

14.strip—— 清除字符串两边空格

>>> ss = "	mimi	"
>>> ss
'\tmimi\t'
>>> ss.strip()
'mimi'

rstrip #清除右边空格
lstrip #清楚左边空格

15.lower——转小写
16.upper——转大写

>>> s = "Tomorrow Is Sunday"
>>> s.lower()
'tomorrow is sunday'
>>> s.upper()
'TOMORROW IS SUNDAY'

17.title——转换字符串为一个符合标题的规则

>>> str = 'my favorite star is xiaozhan'
>>> str.title()
'My Favorite Star Is Xiaozhan'

18.replace——替换字符串

>>> str.replace("xiaozhan","zhuyilong")
'my favorite star is zhuyilong'

下面is判断语句了解会用即可
isalnum #判断是不是由字母和数字组成

>>> a="123abc"
>>> a.isalnum()
True

isalpha #判断是不是由字母组成

>>> a.isalpha()
False
>>> b="abc"
>>> b.isalpha()
True

isdigit’ #判断是不是数字组成

>>> a.isdigit()
False
>>> c="123"
>>> b.isdigit()
True

isupper #判断是不是全大写
islower #判断是不是全小写

>>> str = "my favorite star is xiaozhan"
>>> str.islower()
True
>>> str.isupper()
False

isspace #判断是不是空白字符

>>> l = "a"
>>> l.isspace()
False
>>> l = " "
>>> l.isspace()
True

istitle #判断是不是标题(每个首字母大写)

>>> str = "my favorite star is xiaozhan"
>>> str.istitle()
False
>>> str = "My Favorite Star Is Xiaozhan"
>>> str.istitle()
True
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值