数据类型——字符串类型

# ======================================基本使用======================================
# 1、用途:记录描述性质的状态

# 2、定义方式:在单引号、双引号、三引号内包含一串字符串
msg='hello' # msg=str('hello')

# 数据类型转换:所有类型都可以被str转成字符串类型
# res=str([1,2,3])
# print(res,type(res))

# 3、常用操作+内置的方法
#3.1、按索引取值(正向取+反向取) :只能取

# msg='hello'
# print(msg[0],type(msg[0]))
# print(msg[-1])
# print(msg[-2])

#3.2、切片(顾头不顾尾,步长)

# msg='hello world'
# res=msg[0:3:1] # 0 1 2
# print(res)
# print(msg)

# res=msg[:]
# res=msg[::2] # 0 2 4 6 8 10
# print(res)

#3.3、长度len

# msg='hello world'
# print(len(msg))

#3.4、成员运算in和not in:判断一个子字符串是否存在于大字符串中

msg='kevin is dsb'

# print('kevin' in msg)
# print('dsb' in msg)
# print('aaa' not in msg)
# print(not 'aaa' in msg)

#3.5、移除空白strip: 用来去除字符串左右两边的字符,不指定默认去除的是空格

# msg='      he llo     '
# res=msg.strip()
# print(res,id(res))
# print(msg,id(msg))

#3.6、切分split:针对有规律的字符串,按照某种分隔符切成列表 

info='egon:18:male'
# res=info.split(':')
# print(res,type(res))
# print(res[0],res[1])

# cmd='get|a.txt|33333'
# print(cmd.split('|',1))

#3.6、切分split:针对有规律的字符串,按照某种分隔符切成列表

info='egon:18:male'
# res=info.split(':')
# print(res,type(res))
# print(res[0],res[1])

# cmd='get|a.txt|33333'
# print(cmd.split('|',1))

 用:号作连接符号将纯字符串的列表拼接成一个字符串

l=['egon', '18', 'male']  # 'egon:18:male'
# res=l[0]+':'+l[1]+':'+l[2]
res=':'.join(l)
# print(res)

#3.7、循环

# for item in 'hello':
#     print(item)

#需要掌握的操作 

  

#1、strip,lstrip,rstrip
# print('******egon***********'.strip('*'))
# print('******egon***********'.lstrip('*'))
# print('******egon***********'.rstrip('*'))

#2、lower,upper
# print('Abc123'.lower())
# print('Abc123'.upper())

#3、startswith,endswith
# msg='alex is dsb'
# print(msg.startswith('alex'))
# print(msg.endswith('b'))

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

# res='my name is {name} my age is {age}'.format(age=18,name='egon')
# print(res)

# 了解
# res='my name is {} my age is {}'.format('egon',18)
# res='my name is {0}{1} my age is {1}{1}{1}{1}'.format('egon',18)
# print(res)

#5、split,rsplit
# msg='a:b:c:d'
# print(msg.split(':',1))
# print(msg.rsplit(':',1))

#6、replace
# msg='kevin is kevin is hahahah'
# res=msg.replace('kevin','sb',1)
# print(res)

#7、isdigit
# print('123123'.isdigit()) # 如果字符串是由纯数字组成的,则返回True
# print('123123   '.isdigit())
# print('123123asdf'.isdigit())
# print('12312.3'.isdigit())

# score=input('>>>>: ').strip() #score='asdfasdfasfd'
# if score.isdigit():
#     score=int(score)
#
#     if score >= 90:
#         print('优秀')
#     else:
#         print('小垃圾')
# else:
#     print('必须输入纯数字')

# 了解的操作
#1、find,rfind,index,rindex,count
# print('123 ke123ke'.find('ke'))
# print('123 ke123ke'.rfind('ke'))
# print('123 ke123ke'.index('ke'))
# print('123 ke123ke'.rindex('ke'))

# print('123 ke123ke'.find('xxxx'))
# print('123 ke123ke'.index('xxxx'))
# print('123 ke123ke'.count('ke',0,6))

#2、center,ljust,rjust,zfill
# print('egon'.center(50,'*'))
# print('egon'.ljust(50,'*'))
# print('egon'.rjust(50,'*'))

# print('egon'.rjust(50,'0'))
# print('egon'.zfill(50))

#3、captalize,swapcase,title
# print('abcdef dddddd'.capitalize())
# print('abcAef dddddd'.swapcase())
# print('abcAef dddddd'.title())

#4、is数字系列
num1=b'4' #bytes
num2='4' #unicode,python3中无需加u就是unicode
num3='四' #中文数字
num4='Ⅳ' #罗马数字

# bytes与阿拉伯数字组成的字符串
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())

# 阿拉伯数字组成的字符串
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())

# 阿拉伯数字中文罗马组成的字符串
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())

#5、is其他

  

原文地址:https://www.cnblogs.com/king-home/p/10827257.html