Python 学习2-元组、字符串

三、元组

元组(tuple)创建后不能被修改,元组使用小括号,表使用方括号

(1)元组的创建

tuple1=(1,23,4,5,25,7645,8,64,85)
print(tuple1,type(tuple1)) #(1, 23, 4, 5, 25, 7645, 8, 64, 85) <class 'tuple'>
print(tuple1[2]) #4
print(tuple1[3:4]) #(5,)
print(tuple1[-3:-1]) #(8, 64)
print(tuple1[2:8:3]) #(4, 7645)

tuple2=1
print(type(tuple2)) #<class 'int'>
tuple2=(1)
print(type(tuple2)) #<class 'int'>
tuple2=(1,)
print(type(tuple2)) #<class 'tuple'>
tuple2=[1]
print(type(tuple2)) #<class 'list'>

(2)二维元组

tuple1=((132,4543,312),123,4324,534) 
print(tuple1[0][1]) #4543
print(tuple1[1]) #123

(3)元组的更新和删除
因为元组有不可更改的性质,所以不能直接给元组中元素赋值,这与给它赋值不同

tuple1=(1,2,4,5)
tuple1=tuple1[:2] + (3,) + tuple1[2:]
print(tuple1) #(1, 2, 3, 4, 5)

tuple2=(1, 2, 3, [4, 5, 6])
print(tuple2)  # (1, 2, 3, [4, 5, 6])
tuple2[3][0] = 9
print(tuple2)  # (1, 2, 3, [9, 5, 6])

(4)内置方法
countindex

tuple1=(1,45,457,1,967,6)
print(tuple1.count(1,)) # 2
print(tuple1.index(1,2,5)) # 3

(5)解压元组
从元组中提出元素

  1. 提取全部元素
tuple1=(312,6457,876,(53,3654))
(a,b,c,(d,e))=tuple1
print(a,b,c,(d,e)) #312 6457 876 (53, 3654)
  1. 提取部分元素
    从元组中提取部分元素时,用到通配符[*],可接rest变量,当你不在意rest变量时,也可以用_(*rest表示中间几个变量)
tuple1=(1,45,6,7,432,'gdf',423,9786)
a,b,c,*rest,e,d=tuple1
print(a,b,c,e,d) #1 45 6 423 9786
print(tuple1) #(1, 45, 6, 7, 432, 'gdf', 423, 9786)
print(rest) #[7, 432, 'gdf']

四、字符串

(1)字符串的创建

tuple1 = 'i love Python!'
print(tuple1, type(tuple1)) # i love Python! <class 'str'>

tuple2 = "I love Python!"
print(tuple2, type(tuple2)) # I love Python! <class 'str'>

print(5 + 8)  # 13
print('5' + '8')  # 58

(2)转义字符
字符串前加上r代表原样输出

print('aaa\tbbb') #aaa	bbb
print(r'aaa\tbbb') #aaa\tbbb

三引号你可以多行输入输出,单引号不可以

str1="""dada
eqweqweqeq
eqweqw"""
print(str1) 

(3)字符串的切片与拼接
空格也要占一位
start:
start:stop
start:stop:step

str1='asd sa sdafwe hjkjkhu'
print(str1) #asd sa sdafwe hjkjkhu
print(str1[-1]) #u
print(str1[:7]) #asd sa 
print(str1[:7]+str1[9:]) #asd sa afwe hjkjkhu
print(str1[1:10:2]) #s asa

(4)字符串的内置

  1. capitalize将字符串的第一个字母变成大写
str1='dasdg'
print(str1.capitalize()) #Dasdg
  1. lower将字符串中的大写转换成小写
    upper将字符串中的小写转换成大写
    swapcase将字符串中的大写转换成小写,小写转换成大写
str1='SfsFpy'
print(str1.lower()) #sfsfpy
print(str1.upper()) #SFSFPY
print(str1.swapcase()) #sFSfPY
  1. count数给定字符串出现的次数
    count(’ ‘,beg,end),(’ '是你要计数的字符串,beg是你要开始计数的位置,end是结束计数的位置,同样地,上组限不在内,end最大值为len(str1))
str1='dasddihijiffdd'
print(str1.count('dd',6,13)) #0
print(str1.count('dd',0,13)) #1
  1. endswith
    endswith(’ ‘,beg,end),检查字符串是否以给定字符串结束,是就返回 True,不是就返回False
    startswith
    startswith(’ ',beg,end),检查字符串是否以给定字符串开始,是就返回True,不是就返回False
str1='Dxnhjik'
print(str1.endswith('ik')) #True
print(str1.endswith('IK')) #False
print(str1.startswith('Dx')) #True
print(str1.startswith('dx')) #False
  1. find
    find(’ ‘,beg,end),从字符串左边开始查找给定字符串,若存在,返回其位置,若不存在则返回-1
    rfind
    frind(’ ',beg,end),类似于find函数,但是从右边开始查找
str1="DAXIExiaoxie"
print(str1.find('xi'))  # 5
print(str1.find('ix'))  # -1
print(str1.rfind('xi'))  # 9
  1. isnumeric看字符串中是不是只有数字,若是则返回True,否则返回False
str1='fsdfsd3432'
str2='asdasd'
str3='3127'
print(str1.isnumeric()) #False
print(str2.isnumeric()) #False
print(str3.isnumeric()) #True
  1. ljust
    ljust(width,fillchar)返回字符串左对齐,在长度不够时,用fillchar(默认空格)补充长度至width
    rjust
    rjust(width,fillchar)返回字符串右对齐,在长度不够时,用fillchar(默认空格)补充长度至width
str1='hsdjk432'
print(str1.ljust(19,'0')) #hsdjk43200000000000
print(str1.rjust(19,'0')) #00000000000hsdjk432
  1. lstrip删除字符串左边的空格
    rstrip删除字符串右边的空格
    strip删除字符串两边的空格,当删除给点给字符串时,只能删除首尾的字符串
str1=' sdfsdf '
a=str1.lstrip()
b=str1.rstrip()
c=str1.strip()
d=str1.strip().strip('f')
print(a,len(a),sep='  ') #sdfsdf   7
print(b,len(b),sep='  ') # sdfsdf  7
print(c,len(c),sep='  ') #sdfsdf  6
print(str1,len(str1),sep='  ') # sdfsdf   8
print(d,len(d),sep='  ') #sdfsd  5
  1. endswith
    endswith(‘xxx’,beg,end=len(str1)) 看字符串是否以xxx开头,是就返回True,不就返回False
    startswith
    startswith(‘xxx’,beg,end=len(str1)) 看字符串是否以xxx开头,是就返回True,不就返回False
str1='Dxnhjik'
print(str1.endswith('ik')) #True
print(str1.endswith('IK')) #False
print(str1.startswith('Dx')) #True
print(str1.startswith('dx')) #False
print(str1) 
  1. partition
    partition(‘xxx’),在字符串中找xxx,第一次找到后以xxx为分隔,将字符串分开,(’前边的‘,‘xxx’,‘后边的’)若没找到xxx,则返回(‘原字符串’,’ ‘,’ '),xxx出现
    rpartition
    rpartition(‘xxx’),与partition类似,但是从右边开始找
str1='dsa fsd ffhgpo'
print(str1.partition('s')) #('d', 's', 'a fsd ffhgpo')
print(str1.rpartition('s')) #('dsa f', 's', 'd ffhgpo')
  1. replace
    replace(old,new,max)把字符串old换成new,如果指定max值,重复执行max次
str1='das g sdf dsfo'
print(str1.replace('s','W')) #daW g Wdf dWfo
print(str1.replace('s','W',1)) #daW g sdf dsfo
  1. split
    split(‘xxx’,num)默认以空格分搁开字符串,如果num有值,则分成几块,且不出现xxx
#使用默认分隔符
str1='dhakj.hsgfs gjdkl.jgh'
print(str1.split()) #['dhakj.hsgfs', 'gjdkl.jgh']
#用.当分隔符
print(str1.split('.')) #['dhakj', 'hsgfs gjdkl', 'jgh']

#分隔0次
print(str1.split(' ',0)) #['dhakj.hsgfs gjdkl.jgh']
print(str1.split('.',0)) #['dhakj.hsgfs gjdkl.jgh']
#分隔1次
print(str1.split(' ',1)) #['dhakj.hsgfs', 'gjdkl.jgh']
print(str1.split('.',1)) #['dhakj', 'hsgfs gjdkl.jgh']
#分割2次
print(str1.split(' ',2)) #['dhakj.hsgfs', 'gjdkl.jgh'] 只有一个空格,只能分成这样
print(str1.split('.',2)) #['dhakj', 'hsgfs gjdkl', 'jgh']

#分隔2次,取序数为2的项
print((str1.split('.',2))[2]) #jgh

#分别保存三个项
a,b,c=str1.split('.',2)
print(a) #dhakj
print(b) #hsgfs gjdkl
print(c) #jgh
  1. maketrans
    maketrans(intab,outab),intab是字符串,outab是要把intab转换成的字符串
    translate
    translate(trantab),trantab是上边输出的结果,把str1中有intab的字符串改成outab字符串,并将str1重新输出
str1='fhsjk iolkcz pmc'="
intab='hkozm'
outab='12345'
trantab=str1.maketrans(intab,outab)
print(trantab) #{104: 49, 107: 50, 111: 51, 122: 52, 109: 53}
print(str1.translate(trantab)) #f1sj2 i3l2c4 p5c

(5)字符串格式化
format

str1="{0} and {1}".format('huawei','honor') #位置参数
print(str1) #huawei and honor
str2="{a} and {b}".format(a='huawei',b='honor') #关键字参数
print(str2) #huawei and honor
str3="{0} and {a}".format('huawei',a='honor') #当位置参数和关键字参数同时存在的时候,位置参数要在前边
print(str3) #huawei and honor
str4='{a:.2f}{b}'.format(a=63.498,b='GB') #保留两位小数
print(str4) #63.50GB
str5 = '{0:.2f}{1}'.format(27.658, 'GB')  # 保留小数点后两位
print(str5)  # 27.66GB
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值