一Python的数据类型可以分为可变与不可变两种:
可变类型:值改变,但是id不变,证明就是在改变原值,就是可变类型
如list dict 列表和字典都是可变类型
不可变类型:值改变,id也跟着改变了,证明就是不可变类型,入
Int float str都是不可变类型
一,字符串类型
1、用途: 性别\爱好等描述性质的状态
2、定义方式
# s1="hello" # s1=str(‘hello‘)
#str 可以将任意其他类型都转成str类型
# res=str({‘a‘:1}) #res="{‘a‘:1}"
# 3、常用操作+内置的方法
#优先掌握的操作:(*****)
#1、按索引取值(正向取+反向取) :只能取
取值正向取标号是从0开始递增,反向取标号是从-1开始递减
s1="hello world"
# print(s1[0])
# print(s1[-1])
# print(s1[-3])
# s1[0]=‘H‘
# print(s1)
#2、切片(顾头不顾尾,步长):从大字符串中切出一个子字符串
# s1="hello world"
# res=s1[1:5]
# print(s1)
# print(res)
# print(s1[0:7:1]) #0 1 2 3 4 5 6
# print(s1[0:7:2]) #0 2 4 6
# 取值之后用加号运算符也可以实现切片的功能
# print(s1[-1]+s1[-2]+s1[-3]+s1[-4]+s1[-5])
# print(s1[-1::-1]) # -1 -2
# print(s1[::-1]) # -1 -2
#3、长度len
# s1="hello world"
# print(len(s1)) # 字符的个数
#4、成员运算in和not in:判断一个子字符串是否存在于一个大字符串中
# msg=‘my name is alex,alex is dsb‘
# print(‘alex‘ in msg)
# print(‘egon‘ not in msg)
#5、移除空白strip: 移除字符串左右两边的字符空格
# name=input(‘username>>>: ‘).strip() #name=‘egon ‘
# name=name.strip()
# if name == ‘egon‘:
# print(‘认证成功‘)
# msg=‘ he llo
‘
# res=msg.strip(‘ ‘)
# print(msg)
# print(res)
# msg=‘******hello*************‘
# res=msg.strip(‘*‘)
# print(res)
# msg=‘***&^#***hello***=-/?**‘
# print(msg.strip(‘*&^$/-?#=‘))
#6、切分split: 把一個有規律的字符串按照某個字符進行切分,切成列表
# info=‘root:x:0:0::/root:/bin/bash‘
# res=info.split(‘:‘,maxsplit=-1)
# print(res)
# cmd=‘get|a.txt|3333‘
# res=cmd.split(‘|‘)
# print(res)
# info=‘‘
# userinfo=[‘root‘, ‘x‘, ‘0‘, ‘0‘, ‘‘, ‘/root‘, ‘/bin/bash‘]
# for item in userinfo:
# item+=‘:‘
# info+=item
# info=info.strip(‘:‘)
# print(info,type(info))
# userinfo=[‘root‘, ‘x‘, ‘0‘, ‘0‘, ‘‘, ‘/root‘, ‘/bin/bash‘]
# res=‘:‘.join(userinfo)
# print(res,type(res))
#7、循环
# msg=‘hello‘
# for item in msg:
# print(item)
# 需要掌握的操作(****)
#1、strip,lstrip,rstrip
# print(‘****egon****‘.strip(‘*‘))
# print(‘****egon****‘.lstrip(‘*‘))
# print(‘****egon****‘.rstrip(‘*‘))
#2、lower,upper
# x=‘ABBBBddd1231‘
# print(x.lower())
# print(‘ABBBBddd2123‘.upper())
#3、startswith,endswith
# print(‘alex is sb‘.startswith(‘alex‘))
# print(‘alex is sb‘.startswith(‘al‘))
# print(‘alex is sb‘.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(18,‘egon‘)
# print(msg)
# msg=‘my name is {0} my age is {0}{1}{1}‘.format(18,‘egon‘)
# print(msg)
# x1=‘egon‘
# x2=(‘egon111‘)
# print(x1,x2,type(x1),type(x2))
#5、split,rsplit
# print(‘a:b:c:d:e‘.split(‘:‘,maxsplit=1))
# print(‘a:b:c:d:e‘.rsplit(‘:‘,maxsplit=1))
#6、join
#7、replace
# msg=‘alex is alex hahahah alex‘
# res=msg.replace(‘alex‘,‘SB‘,1)
# print(msg)
# print(res)
#8、isdigit
# print(‘1010101‘.isdigit())
# age=input(‘>>>: ‘)
# if age.isdigit():
# age=int(age)
# if age > 10:
# print(‘too Big‘)
# elif age < 10:
# print(‘too small‘)
# else:
# print(‘you got it‘)
# else:
# print(‘必須輸入數字‘)
# 其他操作(了解即可)
#1、find,rfind,index,rindex,count
# print("abcdefg".find(‘de‘,0,3))
# print("abcdefg".index(‘de‘))
# print("abcdefg".index(‘de‘,0,3))
# print(‘alex is alex‘.find(‘alex‘))
# print(‘alex is alex‘.rfind(‘alex‘))
# print(‘alex is alex‘.count(‘alex‘))
#2、center,ljust,rjust,zfill
# print(‘================%s===============‘ %(‘egon‘))
# print(‘egon‘.center(50,‘*‘))
# print(‘egon‘.ljust(50,‘*‘))
# print(‘egon‘.rjust(50,‘*‘))
# print(‘egon‘.zfill(50))
#3、expandtabs
# print(‘abc\tdef‘.expandtabs(8))
#4、captalize,swapcase,title
# print(‘i am egon‘.capitalize())
# print(‘aAbB‘.swapcase())
# print(‘i am egon‘.title())
#5、is数字系列
num1=b‘4‘ #bytes
num2=u‘4‘ #unicode,python3中无需加u就是unicode
num3=‘壹‘ #中文数字
num4=‘Ⅳ‘ #罗马数字
# ‘‘.isdigit() # bytes,unicode
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())
# ‘‘.isdecimal():unicode
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())
# ‘‘.isnumeric():unicode,羅馬,中文
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())
#6、is其他
# name=‘egon123‘
# print(name.isalnum()) #字符串由字母或数字组成
# name=‘egon‘
# print(name.isalpha()) #字符串只由字母组成
# print(name.islower())
# print(name.isupper())
# print(name.isspace())
# print(name.istitle()
原文地址:https://www.cnblogs.com/1832921tongjieducn/p/10067862.html