python数据类型——字符串类型

定义:

>>> msg = 'hello word'
>>> print(msg, type(msg))
# hello word 

msg = 'hello word' 与 msg = str('hello word)的意思相同

类型转换:

str可将任意其他类型转换成字符串

# 1.整型转字符串
>>> msg_1 = str(1234)
>>> print(msg_1, type(msg_1))
# 1234 

# 浮点型转字符串
>>> msg_2 = str(12.5)
>>> print(msg_2, type(msg_2))
# 12.5 

# 列表转字符串
>>> msg_3 = str(['1','2','3'])
>>> print(msg_3 =, type(msg_3))
# ['1', '2', '3'] 


# 字典转字符串
>>> msg_4 = str({'n1':1,'r2':2})
>>> print(msg_4, type(msg_4))
# {'n1': 1, 'r2': 2} 

内置方法:

1.按照索引取值

1.1正向取

>>> msg = 'hello word'
>>> print(msg[0])
# h

1.2反向取

>>> msg = 'hello word'
>>> print(msg[-1])
d

1.3只能取

因为字符串是不可变类型,所以不能改

>>> msg = 'hello word'
>>> msg[0]=o
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'o' is not defined

2.切片:索引的拓展应用,从大字符串拷贝出小字符串

注:顾头不顾尾

>>> msg = 'hello word'
>>> res = msg[0:5]
>>> print(res)
# hello

2.2步长

>>> msg = 'hello word'
>>> res = msg[0:5:20 # 步长为2,只能取到0、2、4
>>> print(res)
# hlo

2.3反向步长

>>> msg = 'hello word'
>>> res = msg[5:0-1] # 从右往左开始,起始点是5,结束是1(顾头不顾尾),步长为1
>>> print(res)
# olle

2.3拓展

1)利用切片拷贝完整的字符串

>>> msg = 'hello word'
>>> res = msg[:] 
>>> print(res)
# hello word

2) 把字符串进行反转

>>> msg = 'hello word'
>>> res = msg[::-1]
>>> print(res)
# drow olleh

3.长度len

>>> msg = 'hello word'
>>> print(len(msg))
# 10

4.成员运算in和not in

>>> 'yxs' in 'yxs is nb'
True

>>> 'yxs' not in 'yxs is nb'
False

5.去除字符串两边的符号strip

>>> msg = '          yxs          '
>>> res = msg.strip() # 默认去除空格
>>> print(msg) # 不改变原来的值
>>> print(res) # 产生新的值
          yxs          
yxs

5.2 去除其他符号

>>> msg = '*******yxs********'
>>> res = msg.strip('*')
>>> print(res)
yxs

5.3 只能去除两边的符号

>>> msg = '*******yx***s********'
>>> res = msg.strip('*')
>>> print(res)
yx***s

6.切分split: 把一个字符串按照某种分隔符进行切分,得到一个列表

默认空格为分隔符

>>> msg = 'yxs 18 male'
>>> res = msg.split()
>>> print(res)
['yxs', '18', 'male']

6.2 指定分隔符

>>> msg = 'hello,word'
>>> res = msg.split(',')
>>> print(res)
['hello','word']

6.3 指定切分次数

>>> msg = 'yxs:18:male'
>>> res = msg.split(':', 1)
>>> print(res)
['yxs', '18:male'] # 切分了一次,列表里️两个字符串

7.循环

>>> msg = 'hello'
>>> for x in msg:
>>>     print(x)
h
e
l
l
o

8. strip,lstrip,rstrip

strip是去除字符串两边的符号

>>> msg = '          yxs          '
>>> res = msg.strip() # 默认去除空格
>>> print(msg) # 不改变原来的值
>>> print(res) # 产生新的值
          yxs          
yxs

lstrip是去除左边的符号,用法与strip相同

>>> msg = '*******yxs********'
>>> res = msg.lstrip('*')
>>> print(res)
yxs********

rstrip是去除右边的符号,用法与strip相同


>>> msg = '*******yxs********'
>>> res = msg.rstrip('*')
>>> print(res)
********yxs

9. lower,upper

lower:将纯英文字符串转成小写,upper:将纯英文字符串转成大写

>>> msg = 'AaaBBcCC'
>>> res_1 = msg.upper()
>>> res_2 = msg.lower()
>>> print(res_1)
>>> print(res_2)
AAABBCCC
aaabbccc

10.startswith,endswith

startswith:判断字符串的开头,endswith:判断字符串的结尾

>>> res = 'yxs is nb'
>>> print(res.startswith('yxs'))
>>> print(res.endswith('nb'))
True
True

11. split,rsplit:将字符串切成列表

不指定切分次数时是相同的

msg = 'yxs is nb'
res_1 = msg.split()
print(res_1)
msg_2 = msg.rsplit()
print(msg_2)
['yxs', 'is', 'nb']
['yxs', 'is', 'nb']

指定切分次数时是不同的

>>> msg = 'yxs:is:nb'
>>> res_1 = msg.split(':',1)
>>> print(res_1)
['yxs', 'is:nb']

>>> msg_2 = msg.rsplit(':',1)
>>> print(msg_2)
['yxs:is', 'nb']

12. join: 把列表拼接成字符串

>>> msg = ['yxs','18','male'] # 只能是str类型
>>> res = ':'.join(msg)
>>> print(res)
yxs:18:male

13. replace

将字符串中某一个小字符串进行替换

>>> msg = 'you can you up no can no bb'
>>> print(msg.replace('you','YOU'))  # replace(被替换的值,要替换的值)
YOU can YOU up no can no bb

也可以指定替换的个数(顺序是从左到右,若不指定,则全部替换)

>>> msg = 'you can you up no can no bb'
>>> print(msg.replace('you','YOU',1))
YOU can you up no can no bb


14. isdigit 判断字符串是否由纯数字组成

>>> msg = '123123'
>>> print(msg.isdigit())
True

 

你可能感兴趣的:(python数据类型,python,字符串)