字符串类型
二、字符串类型
# ======================================基本使用====================================== # 1、用途:记录名字\性别等描述性质 # 2、定义方式:在''\""\""" """\''' '''内包含一系列的字符 # name='kevin' # name=str('kevin') # 数据类型转换:str可以将任意类型都转成字符串类型 # n=str([1,2,3]) #'[1,2,3]' # print(type(n)) # 3、常用操作+内置的方法 #3.1 优先掌握的操作*****: #1、按索引取值(正向取+反向取) :只能取 # msg='hello world' # print(msg[0]) # print(msg[-1]) # msg[0]='A' #2、切片(顾头不顾尾,步长):从一个大的字符串中切出一个小字符串 # msg='hello world' # print(msg[0:5:1]) # print(msg[0:5:2]) #0 2 4 # info='egon 18' # print(info[0:4]) # 了解 # msg='hello world' # print(msg[-1:-12:-1]) #-1 -2 -3 -4 # print(msg[-1::-1]) #-1 -2 -3 -4 # print(msg[::-1]) #-1 -2 -3 -4 #3、长度len # print(len('ad你好')) #4、成员运算in和not in:判断一个子字符串是否存在于一个大字符串中 # msg='name age sex' # print('sex' in msg) # print('sex' not in msg) #5、移除空白strip:移除字符串左右两边的字符 # msg='*****egon****************' # res=msg.strip('*') # print(res) # msg=' egon ' # print(msg.strip()) # msg='+-*&^%egon-=)^(#' # print(msg.strip('-+*&^%(#=')) # name=input('name>>>: ').strip() # pwd=input('pwd>>>:').strip() # if name == 'egon' and pwd == '123': # print('login successful') # else: # print('user or pwd error') #6、切分split:把一个字符串按照某种分隔符切成一个列表,从而方便取值 # info='egon:18:male' # res=info.split(':') # print(res[0]) # print(res) # info1=res[0]+':'+res[1]+':'+res[2] # info1=':'.join(res) #res中所有的元素必须是str类型 # print(info1) # cmd='get|a.txt|131123123' # print(cmd.split('|')) #7、循环 # msg='hello' # for item in msg: # print(item) # 3.2 需要掌握的操作**** #1、strip,lstrip,rstrip # print('****egon***'.strip('*')) # print('****egon***'.lstrip('*')) # print('****egon***'.rstrip('*')) #2、lower,upper # print('AdaEs1'.lower()) # print('AdaEs1'.upper()) #3、startswith,endswith # print('alex is dsb'.startswith('alex')) # print('alex is dsb'.startswith('al')) # print('alex is dsb'.endswith('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('egon',18)#这种要按顺序输入 # print(msg) # msg='my name is {1}{0}{0}{0} my age is {1}{1}'.format('egon',18) # print(msg) #5、split,rsplit # info='egon:18:male' # print(info.split(':',1)) # print(info.rsplit(':',1)) #6、replace # msg='kevin is dsb kevin' # res=msg.replace('kevin','dsb',1) # print(res) #7、isdigit:当字符串是由纯数字组成时返回True # print('123123'.isdigit()) # age_of_db=18 # inp_age=input('>>: ').strip() # if inp_age.isdigit(): # inp_age=int(inp_age) # print(inp_age == age_of_db) # else: # print('年龄必须是数字') # 3.3 了解的操作 #1、find,rfind,index,rindex,count # find: 查找子字符串在大字符串中的起始位置 # msg='kevin is kevin xxxx sb' # print(msg.find('kevin')) # print(msg.index('kevin')) # print(msg.find('asdf')) # print(msg.index('asdf')) # print(msg.rfind('kevin')) # count:统计子字符串在大字符串中出现的次数 # print('alex kevin alex kevin kevin'.count('kevin')) #2、center,ljust,rjust,zfill # username=input('>>>: ').strip() # print(username.center(50,'*')) # print('egon'.ljust(50,'#')) # print('egon'.rjust(50,'#')) # print('egon'.rjust(50,'0')) # print('egon'.zfill(50)) #3、captalize,swapcase,title # print('abdef adsfasdf'.capitalize()) # print('AbC'.swapcase()) # print('my name is egon'.title()) #4、is数字系列 # print('123213'.isdigit()) # print('Ⅳ'.isnumeric()) # print('贰'.isnumeric()) #5、is其他 # name='My Nmae' # print(name.isalnum()) #字符串由字母或数字组成 # print(name.isalpha()) #字符串只由字母组成 # print(name.isidentifier()) # print(name.islower()) # print(name.isupper()) # print(name.isspace()) # print(name.istitle()) # ======================================该类型总结==================================== # 存一个值 # 有序 # 不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash) # x='a' # print(id(x)) # x+='b' # # print(x) # print(id(x))