Python教程栏目介绍相关数据类型
相关免费学习推荐: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()功能相同,但查找访问为右侧开始
修改: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(): 检查字符串是否是以指