[基本数据类型---字符串类型]
基本数据类型--->>>字符串类型
字符串类型
一、作用:
记录描述性的事物 不可变类型
二、定义:
字符串类型
# msg = 'hello' #msg = str('msg')
# print(type(msg))
三、类型转换:
字符串类型(str)可以把任意类型都转成字符串
# str可以把任意类型都转成字符串
例如:字典等等
# res = str({'a':1})
# print(res,type(res))
四、内置方法
4.1 优先掌握
4.1.1 按照索引取值(正向取+反向取):只能取
# msg = 'hello word'
# 正向取
# print(msg[0])
# print(msg[5])
# 反向取
# print(msg[-1])
# 只能取
# msg[0]='H'
# print(msg) #只能 取 不能改
4.1.2 切片:
索引的拓展应用,从一个大的字符串当中拷贝出一个子字符串
( 顾头不顾尾,步长)
# msg = 'hello word'
# res = msg[0:5:] #顾头不顾尾
# print(res)
#
# 步长
# msg = 'hello word'
# res = msg[0:5:2] #步长为2
# print(res)
# 反向步长 (了解)
# msg = 'hello word'
# res = msg[5:0:-1]
# print(res)
# msg = 'hello word'
# res = msg[::-1] #倒着取
# print(res)
4.1.3 len长度
# msg = 'hello word'
# print(len(msg))
4.1.4 成员运算in和not in
判断一个子字符串是否存在于一个大的字符串中
# print('alex' in 'alex is sb')
# print('alex' not in 'alex is sb') 取返结果
4.1.5 移除空白 strip
# 默认去掉空格
# msg = ' egon '
# res = msg.strip() #strip 是字符串的功能
# print(msg) #不会改变原值
# print(res) #是产生了新值
# msg = '****egon*****'
# res = msg.strip("*")
# print(res)
应用strip
# inp_user = input('your name: ').strip() #strip 默认去除空格
# inp_pwd = input('your pwd: ').strip()
# if inp_user == 'egon' and inp_pwd == '123':
# print('登录成功')
# else:
# print('账号密码错误')
4.1.6 切分split:
把一个字符串按照某种切分符切分,得到一个列表
# info = "egon 18 male"
# res = info.split() #默认按照空格分隔
# print(res)
# 指定分隔符
# info = "egon:18:male"
# res = info.split(':')
# print(res)
# 指定分隔次数(了解)
# info = "egon:18:male"
# res = info.split(':',1) # 1代表只分隔一次
# print(res)
4.1.7 循环
# info = "egon:18:male"
# for x in info:
# print(x)
4.2 需要掌握
4.2.1 strip Lstrip rstrip
# msg = '*****egon*****'
# print(msg.strip("*"))
# print(msg.lstrip("*")) #只去左边
# print(msg.rstrip("*")) #只去右边
4.2.2 Lower,upper
# msg ='aaBbcC'
# res = msg.lower() #全是小写
# res = msg.upper() #全是大写
# print(res)
4.2.3 startswith,endswith
判断是以什么开头,以什么结尾
# print("alex is sb".startswith('alex')) #判断是否以alex开头 结果True
# print("alex is sb".endswith('sb')) #判断是否以sb结尾 结果True
4.2.4 format的三种玩法
l ='my name is {} my age is {}'.format('egon',18)
print(l)
l ='my name is {x} my age is {y}'.format(x='egon',y=18)
print(l)
l ='my name is {0} my age is {1}{0}'.format('egon',18)
print(l)
4.2.5 split,rsplit :将字符串切成列表
# info = 'egon:18:male'
# print(info.split(':',1)) #['egon', '18:male']
# print(info.rsplit(':',1)) #['egon:18', 'male']
4.2.6 join: 把列表拼接成字符串
# l = ['egon','18','male']
# res = ":".join(l) #按照某个分隔符,把元素全为纯字符串的列表,拼接成一个大字符串
# print(res)
4.2.7 replace
# msg = 'you can you up no can no bb'
# print(msg.replace('you',"YOU")) #把字符串中所有的小写you 替换成大写YOU
# print(msg.replace('you',"YOU",1)) #只改一个
4.2.8 isdigit 判断字符串是否由数字组成
# l = '11'.isdigit()
# print(l) #结果为Ture
# l = '11.3'.isdigit()
# print(l) #结果为false 必须为纯数字
应用 例如:
# age = input('请输入年龄: ').strip()
# if age.isdigit(): #判断是否输入的为数字
# age = int(age)
# if age >18:
# print("猜大了")
# elif age < 18:
# print("猜小了")
# else:
# print("猜对了")
# else: #如果输入的不是数字 则:↓
# print("只能输入数字,智障")
其他操作~~~
# 1.find,rfind,index,rindex,count
# 1.1 find:从指定范围内查找子字符串的起始索引,找得到则返回数字1,找不到则返回-1
>>> msg='tony say hello'
>>> msg.find('o',1,3) # 在索引为1和2(顾头不顾尾)的字符中查找字符o的索引
1
# 1.2 index:同find,但在找不到时会报错
>>> msg.index('e',2,4) # 报错ValueError
# 1.3 rfind与rindex:略
# 1.4 count:统计字符串在大字符串中出现的次数
>>> msg = "hello everyone"
>>> msg.count('e') # 统计字符串e出现的次数
4
>>> msg.count('e',1,6) # 字符串e在索引1~5范围内出现的次数
1
# 2.center,ljust,rjust,zfill
>>> name='tony'
>>> name.center(30,'-') # 总宽度为30,字符串居中显示,不够用-填充
-------------tony-------------
>>> name.ljust(30,'*') # 总宽度为30,字符串左对齐显示,不够用*填充
tony**************************
>>> name.rjust(30,'*') # 总宽度为30,字符串右对齐显示,不够用*填充
**************************tony
>>> name.zfill(50) # 总宽度为50,字符串右对齐显示,不够用0填充
0000000000000000000000000000000000000000000000tony
# 3.expandtabs
>>> name = 'tony\thello' # \t表示制表符(tab键)
>>> name
tony hello
>>> name.expandtabs(1) # 修改\t制表符代表的空格数
tony hello
# 4.captalize,swapcase,title
# 4.1 captalize:首字母大写
>>> message = 'hello everyone nice to meet you!'
>>> message.capitalize()
Hello everyone nice to meet you!
# 4.2 swapcase:大小写翻转
>>> message1 = 'Hi girl, I want make friends with you!'
>>> message1.swapcase()
hI GIRL, i WANT MAKE FRIENDS WITH YOU!
#4.3 title:每个单词的首字母大写
>>> msg = 'dear my friend i miss you very much'
>>> msg.title()
Dear My Friend I Miss You Very Much
# 5.is数字系列
#在python3中
num1 = b'4' #bytes
num2 = u'4' #unicode,python3中无需加u就是unicode
num3 = '四' #中文数字
num4 = 'Ⅳ' #罗马数字
#isdigt:bytes,unicode
>>> num1.isdigit()
True
>>> num2.isdigit()
True
>>> num3.isdigit()
False
>>> num4.isdigit()
False
#isdecimal:uncicode(bytes类型无isdecimal方法)
>>> num2.isdecimal()
True
>>> num3.isdecimal()
False
>>> num4.isdecimal()
False
#isnumberic:unicode,中文数字,罗马数字(bytes类型无isnumberic方法)
>>> num2.isnumeric()
True
>>> num3.isnumeric()
True
>>> num4.isnumeric()
True
# 三者不能判断浮点数
>>> num5 = '4.3'
>>> num5.isdigit()
False
>>> num5.isdecimal()
False
>>> num5.isnumeric()
False
'''
总结:
最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景
如果要判断中文数字或罗马数字,则需要用到isnumeric。
'''
# 6.is其他
>>> name = 'tony123'
>>> name.isalnum() #字符串中既可以包含数字也可以包含字母
True
>>> name.isalpha() #字符串中只包含字母
False
>>> name.isidentifier()
True
>>> name.islower() # 字符串是否是纯小写
True
>>> name.isupper() # 字符串是否是纯大写
False
>>> name.isspace() # 字符串是否全是空格
False
>>> name.istitle() # 字符串中的单词首字母是否都是大写
False