三、元组

元组(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

Python 学习2-元组、字符串相关推荐

  1. Python 学习笔记 元组 xxxxxxx XXXXXXXXXX

    Python 学习笔记 元组 xxxxxxx XXXXXXXXXX print("=" * 20) dimensions = (200, 50) print(dimensions[ ...

  2. Python学习week4-python元组与字符串

    1.python数据结构之元组 1.1.元组特点 (1)元组特点:有序,不可变:可以理解为不可变的列表: (2)元组与列表的不同: # 元组tuple,可hash,不可变数据类型,()表示:一般元组用 ...

  3. Learning python学习总结之字符串方法

    总结下最近学习lerning python这本书的字符串部分的一些收获吧. 一.原始字符串 在普通字符串前加'r'即成为原始字符串,特点是抑制转义,即在原始字符串中'\n'这种转义字符串没有特殊含义了 ...

  4. Python学习day01_变量字符串与随机数

    Python学习 Python学习_day01 1.1 一个猜数字的小游戏 1.2 Python内置函数 1.3变量 Variable 1.4 字符串 String 1.5 转义字符 1.6 原始字符 ...

  5. Python学习笔记——元组

    目录 元组 元组的创建 元组数据的添加 元组的删除 元组 在python中是没有数组这个概念的,因为python的变量没有数据类型,而数组里存放的必须是同种类型的数据,所以准确的来说,在python中 ...

  6. [Python学习] 专题三.字符串的基础知识

            在Python中最重要的数据类型包括字符串.列表.元组和字典等.该篇主要讲述Python的字符串基础知识. 一.字符串基础         字符串指一有序的字符序列集合,用单引号.双引 ...

  7. Python学习笔记:字符串和编码

    前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...

  8. Python 学习之元组

    元组:不可修改的序列 tuple(读音是 /tuːpəl/ 或 /tʌpəl/)与列表一样,元组也是序列,唯一的差别在于元组是不能修改的(你可能注意到了,字符串也不能修改).元组语法很简单,只要将一些 ...

  9. 《Python学习之路 -- 字符串的方法》

    在前面已经提到Python中的字符串了,本文来列举介绍字符串的方法,我将字符串的方法分为以下几类: ①查询方法 str.find(target,start=None,end=None) 该方法用于查询 ...

  10. Python学习 Day 3 字符串 编码 list tuple 循环 dict set

    字符串和编码 字符 ASCII Unicode UTF-8 A 1000001 00000000 01000001 1000001 中 x 01001110 00101101 11100100 101 ...

最新文章

  1. 【js】v-for 的一些用法 :class {{index}}
  2. javascript es6 属性 __proto__ prototype 原型链 简介
  3. JQuery学习系列(九)AJAX
  4. 【原创·总结】影响sql查询性能的因素
  5. Ubuntu14.04创建WiFi热点
  6. 移动网流量用户身份识别系统的源代码_真武庙车辆识别系统安装效果图
  7. Linux 网络编程 TCP/UDP编程
  8. PHP架构师成长路线,PHP架构师要求
  9. 13丨性能测试场景:如何进行场景设计
  10. 中易云嵌入式网关丨性能卓越+性价比高+应用场景丰富
  11. Unreal Engine 4 渲染目标(Render Target)教程 之 使用渲染目标绘制(下)
  12. 新手必备pr 2021快速入门教程「三」素材的导入与管理
  13. ExoPlayer播放器 开发者指南(官方权威指南译文)
  14. Oracle索引的理解
  15. C语言磁盘文件由,C语言对磁盘文件进行快速排序简单实例
  16. iptables添加IP段白名单
  17. 采购计算机硬件的理论,计算机硬件采购合同要点分析.doc
  18. Google-Guice入门介绍
  19. h5网页服务器选择,h5制作选择虚拟主机还是服务器
  20. 演化计算基准函数(Python版)

热门文章

  1. PPT:PowerPoint to Flash SDK:SWF
  2. 【集训队互测】ayq 三道题
  3. RT-Thread 应用方面的一点总结(二)
  4. 流媒体学习之路(mediasoup)——Worker(c++)libuv(番外)
  5. LeetCode 0799. 香槟塔
  6. 根据两点经纬度计算方位角
  7. mysql怎么设置id自动编号_MySQL中实现ID编号自动增加的方法
  8. python切片逆序_python 中倒序切片
  9. windows自带桌面管理工具使用方法
  10. 家庭局域网的组建(2台或2台以上)