python in 详解_Python最详细之数据类型讲解

本文详细介绍了Python中的数据类型,包括字符串、列表、元组、字典和集合的操作方法。重点讲解了各种数据类型的特性,如字符串的查找、修改、大小写转换,列表的增删改查,元组的查找,字典的增删改查以及集合的增删操作。此外,还涵盖了Python中的一些常用方法,如`append()`、`extend()`、`remove()`、`in`、`not in`等。文章最后提到了生成器的概念及其创建方式,强调了生成器在内存管理和效率上的优势。
摘要由CSDN通过智能技术生成

Python教程栏目介绍相关数据类型

b4ab371bd34a9e6d6f6cbc0f6a6be13e.png相关免费学习推荐:python教程(视频)

字符串

字符串的坑:

三引号的字符串如果中间没有双引号的字符串,会在解释器中输出为双引号

三引号的字符串如果中间有双引号的字符串,会在解释器中输出为单引号

字符串是存储在内存中

字符串的操作:

输入:input()

输出:print()

索引:str[index]

取地址: id(str)

切片:str[start: end: step] (负数步长 -- 倒序选取; 选取方向需一致,否则无法选取数据)

查找:find():语法:str.find(substr, start, end)注意:返回值是第一次出现的索引值;如果子串不存在返回-1,不会报错.

例如:mystr = 'hello world and itcast and itheima and Python' print(mystr.find('and')) # 12 print(mystr.find('and',15,30)) # 23 print(mystr.find('ands')) # -1 ands子串不存在 index():

语法: str.index(substr, start, end)返回值是第一次出现的索引值;如果子串不存在,会报错例如:mystr = 'hello world and itcast and itheima and Python'print(mystr.index('and')) # 12 print(mystr.index('and',15,30)) # 23print(mystr.index('ands')) #如果index查找子串不存在,报错 count(): 统计子串出现的次数语法: str.index(substr, tart, end)注意: 返回值是子串出现的次数,如果子串不存在返回0,不会报错例如:mystr = 'hello world and itcast and itheima and Python' print(mystr.count('and',15,30)) # 1 print(mystr.count('and')) # 3print(mystr.count('ands')) # 0 rfind(): 和find()功能相同,但查找方向为右侧开始rindex(): 和index()功能相同,但查找访问为右侧开始

9042f318a1cca481d50643dafb934c25.png

修改:replace() : 替换语法: str.replace(旧子串,新子串, 替换次数)注意:如果不写替换次数,默认会将旧子串全部替换替换次数如果超出子串出现次数,则替换次数为该子串出现的次数。调用replace函数后,发现原有字符串的数据并没有做到修改,修改后的数据是replace函数的返回值 说明: 字符串是不可变数据类型例如: mystr = 'hello world and itcast and itheima and Python' new_str = mystr.replace('and','he') new_str = mystr.replace('and','he',1) new_str = mystr.replace('and','he',10) split() : 分割语法:str.split(分割字符, num)注意:返回值是一个列表, 丢失分割字符num 表示的是分隔字符出现的次数,即将来返回数据个数为num+1个

例如mystr = 'hello world and itcast and itheima and Python' list1 = mystr.split('and') list1 = mystr.split('and',2) join():

合并语法:字符或子串. join(多字符组成的序列)注意:用一个子串或子串合并字符串,即是将多个字符串合并为一个新的字符串例如:mylist = ['aa','bb','cc'] new_str = '...'.join(mylist) print(new_str)

大小写转换capitalize() : 将字符串第一个字符转换成大写语法:str.capitalize()注意:capitalize()函数转换后,只字符串第一个字符大写,其他的字符全都小写

例如:mystr = 'hello world and itcast and itheima and Python' new_str = mystr.capitalize() print(new_str) title(): 将字符串每个单词首字母转换成大写语法: str.title()

例如:mystr = 'hello world and itcast and itheima and Python' new_str = mystr.title() print(new_str) lower(): 将字符串中大写转小写语法: str.lower()注意:全部转为小写

例如: mystr = 'hello world and itcast and itheima and Python'new_str = mystr.lower() print(new_str) upper(): 将字符串中小写转大写语法: str.upper()注意:全部转为小写

例如:mystr = 'hello world and itcast and itheima and Python' new_str = mystr.upper() print(new_str)

删除前后空格lstrip(): 删除字符串左侧空白字符语法: str.lstrip()例如:mystr = ' hello world and itcast and itheima and Python ' new_str = mystr.lstrip() print(new_str) rstrip(): 删除字符串右侧空白字符语法: str.rstrip()

例如:mystr = ' hello world and itcast and itheima and Python ' new_str = mystr.rstrip() print(new_str) strip(): 删除字符串两侧侧空白字符语法: str.strip()

例如:mystr = ' hello world and itcast and itheima and Python ' new_str = mystr.strip() print(new_str)

字符串对齐ljust(): 返回一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长度的新字符串语法: str.ljust(长度,填充字符)例如:mystr = "hello"print(mystr.ljust(10,'.')) 效果为: 'hello.....' rjust(): 返回一个原字符串右对齐,并使用指定字符(默认空格)填充至对应长度的新字符串语法: str.rjust(长度,填充字符)

例如:mystr = "hello" print(mystr.rjust(10,'.')) 效果为: '.....hello' center():返回一个原字符串居中对齐,并使用指定字符(默认空格)填充至对应长度的新字符串语法: str.center(长度,填充字符)例如: mystr = "hello" print(mystr.conter(10,'.')) 效果为: '..hello...'

判断开头结尾startswith(): 检查字符串是否是以指

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值