字符串类型str
======================================基本使用======================================
1、用途:记录描述性值的状态,比如名字、性别等

2、定义方式
msg='hello world' #msg=str('hello world')

类型转换: 可以把任意类型专场字符串类型
res1=str(10)
res2=str(10.3)
res3=str([1,2,3])
res4=str({'x':1}) #res4="{'x':1}"

print(type(res1))
print(type(res2))
print(type(res3))
print(type(res4))


3、常用操作+内置的方法
优先掌握的操作:(*****)
1、按索引取值(正向取+反向取) :只能取
msg='hello world'

print(type(msg[0]))#取h
print(msg[-1])#取d

msg[0]='H'#报错,'H'无法赋值给 h,字符串是不可变类型

 
2、切片(顾头不顾尾,步长)
msg='hello world'
print(msg[0]+msg[1]+msg[2])# h e l
print(msg[0:5])#0 1 2 3 4,h e l l o 默认步长为1
print(msg[0:5:2]) #0 2 4,h l o
print(msg[0:]) #不写结尾,直接到头,hello world
print(msg[:]) #都省略,:不能省,代表全部输出 hello word
反向索引,改步长,

print(msg[-1:-5:-1]) #-1 -2 -3 -4 反向从-1开始计数

print(msg[::-1]) #-1 -2 -3 -4


3、长度len:统计的是字符的个数
msg='h你d'
print(len(msg))

4、成员运算in和not in:判断一个子字符串是否存在与一个大字符串中
msg='hello world'
print('ho' in msg)#ho不连续,所以结果位false
print('ho' not in msg)#True

5、移除空白strip:移除字符串左右两边的某些字符
msg=' hello '

print(msg.strip(' '))#' '中间有空格
print(msg.strip())#默认去空格
print(msg)
引用: 去"*"
 msg='***h**ello**********'
print(msg.strip('*'))#只能去掉左右两边,输出h**ello
 msg='*-=+h/ello*(_+__
print(msg.strip('*-=+/(_'))

6、切分split: 把有规律的字符串切成列表从而方便取值
info='egon:18:180:150'
res=info.split(':',1)#以:为分割,从左到右分割一次
print(res)#输出['egon','18:180:150']
 info='egon:18:180:150'
res=info.split(':')#以:为分割
print(res)#输出[egon,18,180,150]

s1=res[0]+':'+res[1]+':'+res[2]+':'+res[3]
s1=''
for item in res:
s1+=item
print(s1)


s1=':'.join(res)#以:拼接
print(s1)

':'.join([1,2,3,4,5])#报错,只能拼接字符串


注意:需要掌握的技能补充
1、strip,lstrip,rstrip
msg='*****hello****'
print(msg.strip('*'))
print(msg.lstrip('*'))#去左边*
print(msg.rstrip('*'))#去右边*

2、lower,upper
msg='AaBbCc123123123'
print(msg.lower())#字母全变小写
print(msg.upper())#字母全变大写

3、startswith,endswith
msg='alex is dsb'
print(msg.startswith('alex'))#判断是不是以alex开头

print(msg.endswith('sb'))#判断是不是以sb结尾

4、format的三种玩法
msg='my name is %s my age is %s' %('egon',18)
print(msg)

msg='my name is {name} my age is {age}'.format(age=18,name='egon')#为指定参数传值
print(msg)

了解
msg='my name is {} my age is {}'.format(18,'egon')#丧失了format的功能
msg='my name is {0}{0} my age is {1}{1}{1}'.format(18,'egon')#{0}代表索引后面括号里的18,也就是说重复两次18,重复三次egon
print(msg)


5、split,rsplit#切分
cmd='get|a.txt|33333'
print(cmd.split('|',1))
print(cmd.rsplit('|',1))从右边开始切


6、replace 替换
msg='kevin is sb kevin kevin'
print(msg.replace('kevin','sb',2))

7、isdigit #当字符串内为纯数字时结果为True
res='11111'
print(res.isdigit())
int(res)

age_of_bk=18
inp_age=input('your age: ').strip()
if inp_age.isdigit():
inp_age=int(inp_age) #int('asdfasdfadfasdf')
if inp_age > 18:
print('too big')
elif inp_age < 18:
print('to small')
else:
print('you got it')
else:
print('必须输入纯数字')



了解内容
1、find,rfind,index,rindex,count
print('xxxkevin is sb kevin'.find('kevin'))#从左找索引,输出3
print('xxxkevin is sb kevin'.index('kevin'))#同上
print('xxxkevin is sb kevin'.rfind('kevin'))#从右找索引,输出15
print('xxxkevin is sb kevin'.rindex('kevin'))同上


res='xxxkevin is sb kevin'.find('kevasdfsadfin')
print(res)#找不到输出-1

res='xxxkevin is sb kevin'.index('kevasdfsadfin')#找不到,报错



print('kevin is kevin is kevin is sb'.count('kevin'))#出现次数

2、center,ljust,rjust,zfill #控制显示样式
print('egon'.center(50,'*'))#居中显示egon
print('egon'.ljust(50,'*'))#左对齐
print('egon'.rjust(50,'*'))#右对齐
print('egon'.zfill(50))#右对齐,用0填充,

3、captalize,swapcase,title
print('my name is kevin'.capitalize()) #第一个字母大写,M大写,其他不变
print('AaBbCc'.swapcase())#大小写反转
print('my name is kevin'.title())#所有单词首字母大写

4、is其他
name='egon123'
print(name.isalnum()) #字符串由字母或数字组成
print(name.isalpha()) #字符串只由字母组成

print(name.islower())#字符串字母是否全部小写
print(name.isupper())#
字符串字母是否全部小写

name=' '
print(name.isspace())#全字符串部空格?
msg='I Am Egon'
print(msg.istitle())#全部首字母大写?




# ======================================该类型总结====================================
# 存一个值

# 有序

# 不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
# x='aaa'
# print(id(x))
# x='bbb'
# print(id(x))


 

 


posted on 2018-11-22 19:43  飞花飘絮  阅读(166)  评论(0编辑  收藏  举报