字符串
1.认识字符串
字符串是python中常用的数据类型,一般使用引号来创建字符串。
a = 'hello word'
print(a, type(a))
b = "zhangshan"
print(b, type(b))
c = '''ttt'''
print(c, type(c))
d = """hhhh"""
print(d, type(d))
如果字符串中有引号可使用 \ 转义
字符串输出
name = '张三'
print('我叫%s' %name)
print(f'我叫{name}')
字符串输入
username = input('请输入账号:')
print(f'您的密码为{username}', type(username))
2.下标
下标 又叫 索引,就是编号,下标的作用就是通过下标快速找到对应的数据
str1 = 'abcdef'
print(str1[0]) # a
2.切片
切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片的操作
序列[开始位置下标: 结束位置下标:步长]
1.不包含结束位置下标位置对应的数据,正负整数均可
2.步长是选取间隔,正负整数均可,默认步长为1
str1 = 'abcdefghigklm'
print(str1[2: 6: 1]) # cdef
print(str1[2: 6]) # cdef
print(str1[: 6]) # abcdef 如果不屑开始,默认0开始
print(str1[:]) # abcdefghigklm 如果不写开始和结束,默认选取所有
print(str1[::-1]) # mlkgihgfedcba 如果步长为负数,表示倒叙选取
print(str1[-8:-2]) # fghigk 下标 -2 表示最后一个数据,依次向前类推
print(str1[-8:-2:-1]) # 不能选取 从 -8 到 -2 结束,选取方向从左到右,但是-1步长是从右侧到左侧选取
print(str1[-2:-8:-1]) # lkgihg
# 如果选取方向(下表开始到结束的方向) 和步长的方向冲突,则无法选取数据
3.常用方法
字符串的常用操作方法有查找、修改和判断三类
1.查找
所谓字符串查找方法即是查找子串在字符串中的位置或出现的次数
find(): 检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下表,否则返回-1
index(): 检测某个子串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则则报异常
rfind(): 和find() 功能相同,但查找方向为右侧开始
rIndex(): 和index() 功能相同,但查找方向为右侧开始
count(): 返回某个子串在字符串中出现的次数
用法:字符串序列.find(子串,开始位置下标,结束位置下标)
注:开始位置和结束位置下标可以省略,表示在整个字符串序列中查找
str1 = 'abcdefgabcdef'
print(str1.find('bcd')) # 1
print(str1.find('cd', 3, 4)) # -1
print(str1.index('bcd')) # 1
# print(str1.index('bcd', 3,4)) # 报错
print(str1.count('bcd')) # 2
print(str1.count('bcd', 6, 15)) # 1
print(str1.rfind('bcd')) # 8
print(str1.rindex('bcd')) # 8
2.修改
所谓修改字符串,就是通过函数的形式修改字符串中的数据
replace(): 替换
split(): 按照指定字符分割字符串
join(): 用一个字符或子串合并字符串,即是将多个字符串合并为一个新的字符串
语法:字符串序列.replace(旧字串,新子串,替换次数)
字符串序列.split(分割字符, num)
字符或字符串.join(多字符串组成的序列)
注:替换次数如果超出子串出现次数,则替换次数为该字串出现次数
num 表示的是分割字符出现的次数
str1 = 'hello world abc and abd and abg'
replaceStr = str1.replace('a', 'A')
replaceStr2 = str1.replace('a', 'B', 2)
splitStr = str1.split(' ')
splitStr2 = str1.split(' ', 2)
print(replaceStr) # hello world Abc And Abd And Abg
print(replaceStr2) # hello world Bbc Bnd abd and abg
print(splitStr) # ['hello', 'world', 'abc', 'and', 'abd', 'and', 'abg']
print(splitStr2) # ['hello', 'world', 'abc and abd and abg']
list = ['a', 'b', 'c']
joinStr = ','.join(list)
print(joinStr) # a,b,c
# 字符串是不可变数据类型
capitalize(): 将字符串第一个字符转成大写
title(): 将字符串每个单词首字母转换成大写
lower(): 将字符串中大写转小写
upper(): 将字符串中大写转小写
str1 = 'hello world abc and abd and abg'
print(str1.capitalize()) # Hello world abc and abd and abg
print(str1.title()) # Hello World Abc And Abd And Abg
print(str1.title().lower()) # hello world abc and abd and abg
print(str1.upper()) # HELLO WORLD ABC AND ABD AND ABG
lstrip(): 删除字符串左侧空白字符
rstrip(): 删除字符串右侧空白字符
strip():删除字符串两侧空白字符
ljust(): 返回一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长度的新字符串
rjust(): 返回一个原字符串右对齐,并使用指定字符(默认空格)填充至对应长度的新字符串
center(): 返回一个原字符串居中对齐,并使用指定字符(默认空格)填充至对应长度的新字符串
用法: 字符串序列.ljust(长度, 填充字符)
str1 = 'hello'
print(str1.ljust(20, 'a')) # helloaaaaaaaaaaaaaaa
print(str1.rjust(20, 'b')) # bbbbbbbbbbbbbbbhello
print(str1.center(20, 'c')) # ccccccchellocccccccc
3.判断
所谓判断即是判断真假,返回的结果是布尔型数据类型: True或 False
startswith(): 检查字符串是否是以指定字串开头,如果设置开始和结束下标,则在指定范围内检查
endswith(): 检查字符串是否是以指定字串结束
isalpha():判断字符串至少有一个字符并且所有字符都是字母
isdigit():判断字符是否只包含数字
isalnum(): 判断字符串至少有一个字符并且所有字符都是字母或者数字
isspace(): 判断字符串中只包含空白
子字符串序列.startswith(子串,开始位置下标,结束位置下标)