python从入门到就业-数据结构-字符串

定义

原始字符串:字符串里面是什么内容就是什么内容
print('aaaa', type('aaaa'))
print("aaaa", type("aaaa"))
print('''aaaa''', type('''aaaa''')) #可用于注释/直接跨行书写
print("""aaaa""", type("""aaaa""")) #可用于注释/直接跨行书写
#输出均为aaaa <class 'str'>
转义符:通过转换某个特定的字符,使其具有特殊的含义
print("\"")  #\"双引号 
# "
print("\'") #\'单引号
# '
print("\n") #\n换行
print("\t") #横向制表符
a = "hello "\
"world"
print(a) #\为续行符,但是一个续行符只能续一行 
#hello world
#跨行的第二种方法,小括号括起来
a = ("hello "
     "world")
print(a) #hello world  
原始字符串只需要在字符串前面加r(可以避免转义字符
print(r"\n") # \n
print(r"\t") # \t

单双引号嵌套,就近原则,可以避免使用转义字符

print('hello "world"') # hello "world"

字符串基本操作

拼接

#方法一 "+"
a = "hello "
b = "world"
print(a + b) # hello world
#二 保证在一行
a = "hello "     "world"
print(a) # hello world
#三 使用填充占位符%s
a = "hello, %s%d"%("world",888)
print(a) # hello, world888
#四 字符串乘法
print("hello\t"*5) # hello    hello    hello    hello    hello    

切片

python的索引从 0 开始
索引不要越界
-1是指倒着数

不允许从头部跳到尾部,或者从尾部跳到头部

a = "01234"
print(a[-1]) #4
#切片
print(a[0:2]) #[a:b:c] 索引到[a,b) c是步长 默认为1
print(a[0:2:2]) #0 a默认为0 b默认为len(a) - 1
print(a[0:2:-1]) #空 第一个输出0 然后需要从头部跳到尾部
print(a[2:0:-1]) #21
反转字符串
a = "01234"
print(a[: : -1]) #43210

查找计算

a = "01234"
print(len(a)) #len(a) 计算a的长度  5
b = "01234\n"
print(len(b)) #6 转义符算一个

#方法
#find(sub, start = 0, end = len(str)) 
#查找子串函数 找到了返回字串的起始位置  找不到就返回-1
name = "luo luo luo ya ya ya ha ha ha"
print(name.find("ya")) #12  找到之后会立即停止!
print(name.find("luo", 10)) #-1
print(name.find("luo", 7 , 9)) #-1 [start, end)!end取不到

#rfind
#从右往左查找
print(name.rfind("ya")) #18  

#index 与find其余完全相同 找不到报错!
print(name.index("ya")) #12  找到之后会立即停止!
#print(name.index("yay")) #ValueError: substring not found

#rindex 与rfind其余完全相同 从右往左查找!
print(name.rindex("ya")) #18 
#print(name.rindex("yya")) #ValueError: substring not found

#count(sub, start = 0, end = len(str))
#查找并返回字串出现的次数
print(name.count("ya")) #3

替换操作

name = "niko niko hello world"
#replace(old, new, count) #count缺省则默认替换所有的,也可以限定个数
print(name.replace("niko", "hello")) #hello hello hello world
print(name.replace("niko", "hello", 1)) #hello niko hello world
print(name)#niko niko hello world 并不会影响原有字符串

#capitalize()
#将字符串中的首字母变为大写
print(name.capitalize()) #Niko niko hello world
print(name) #niko niko hello world 不会修改原有的字符串本身

#title()
#将字符串当中每一个单词的首字母都daxie
print(name.title())#Niko Niko Hello World
print(("helloworld").title()) #Helloworld
print(name) #niko niko hello world 不会修改原有的字符串本身
print("hello world".title()) #Hello World

#lower()
#将字符串每个字符都变为小写
#不会修改原有的字符串本身
print("HWlsdoDGA".lower()) #hwlsdodga

#upper()
#将字符串每个字符都变为大写
#不会修改原有的字符串本身
print("HWlsdoDGA".upper()) #HWLSDODGA

填充压缩

b = "0123456"
#ljust(width, fillchar) 
#根据给定的字符长度填充 l表示向左靠齐
#width为字符串的长度 fillchar是长度不够时候需要填充的字符,填充的字符串只能有一个字符
#不会修改原有的字符串本身
print(b.ljust(10, "7")) #0123456777
print(len(b.ljust(10))) #10

#rjust(width, fillchar) 
#根据给定的字符长度填充 r表示向右靠齐
#width为字符串的长度 fillchar是长度不够时候需要填充的字符,填充的字符串只能有一个字符
#不会修改原有的字符串本身
print(b.rjust(10, "7")) #7770123456
print(b.rjust(10)) #   0123456

#center(width, fillchar) 
#根据给定的字符长度填充 原字符串居中
#width为字符串的长度 fillchar是长度不够时候需要填充的字符,填充的字符串只能有一个字符
#不会修改原有的字符串本身
#不对分时候,左边少 右边多
print(b.center(10, "7")) #7012345677
print(b.center(10)) # 0123456    左1右2

#lstrip(chars)
#移除所有源字符串的指定chars,默认为空白字符 比如空格 或者\n \t
#l表示 只移除左端
#不改变原有字符
print("   hello".lstrip()) #hello
print("hello   ".lstrip()) #hello   右边没有效果
print("hello".lstrip("h")) #ello
print("\nhello".lstrip()) #hello
print("\thello".lstrip()) #hello
print("hello".lstrip("hel")) #o 和单个字符移除效果相同
print("hello".lstrip("hlo")) #ello

#rstrip(chars)
#移除所有源字符串的指定chars,默认为空白字符 比如空格 或者\n \t
#l表示 只移除右端
#不改变原有字符
print("hello   ".rstrip()) #hello
print("   hello".rstrip()) #   hello
print("hello".rstrip("o")) #hell
print("hello\n".rstrip()) #hello
print("hello\t".rstrip()) #hello
print("hello".rstrip("ole")) #h和单个字符移除效果相同
print("hello".rstrip("oeh")) #hell
print("hello".rstrip("oel")) #h chars是否能够从右侧连起来

分割拼接

#split(sep, maxsplit)
#将大字符串分割成小字符串 返回分割后的子字符串的列表
#sep是所依据的分隔符 maxsplit是最多分割的个数 这里是分隔符的个数!
#2个分割完之后是3份
#不会修改原字符串
print("2023-2-21".split("-")) #['2023', '2', '21']
print("2023-2-21".split("-", 2)) #['2023', '2', '21']
print("2023-2-21".split("-", 1)) #['2023', '2-21']
print("2023-2-21".split("8", 1)) #['2023-2-21']

#partition(sep)
#根据指定的分割符,返回分隔符左侧内容,中间内容,右侧内容
#返回元组  多个分隔符时候,只从第一个分隔符进行分割,从左侧找
#若不存在分隔符时候,返回元组,第一个元素是整个字符串 第二三个都是空字符串
#不修改原字符串
print("2023-2-21".partition("-")) #('2023', '-', '2-21')
print("2023-2-21".partition("1")) #('2023-2-2', '1', '')
print("2023-2-21".partition("9")) #('2023-2-21', '', '')

#rpartition(sep)
#从右侧找
#若不存在分隔符时候,返回元组,第2个元素是整个字符串 第1,2个都是空字符串
#不修改原字符串
print("2023-2-21".rpartition("-")) #('2023-2', '-', '21')
print("2023-2-21".rpartition("1")) #('2023-2-2', '1', '')
print("2023-2-21".rpartition("9")) #('', '', '2023-2-21')

#splitlines(keepends)
#按照换行符\r,\n将字符串拆成多个元素,保存到列表中
#keppends是否保留换行符\r,\n bool类型 默认为False
print("today\nhello\rworld".splitlines()) #['today', 'hello', 'world']
print("today\nhello\rworld".splitlines(True)) #['today\n', 'hello\r', 'world']

#join(iterable) 
#iterable是可迭代对象 字符串、元组、列表 凡是可以用for循环的对象都是可迭代对象
#根据指定字符串,给定的可迭代对象,进行拼接,得到拼接后的字符串
#返回拼接好的字符串
items = ["1", "2", "3"]
print("-".join(items)) #1-2-3 和split相反作用

判定操作

#isalpha()
#字符串是空串False  是否所有的字符都是字母 不包括数字、特殊符号、标点、转义字符、空格等
#不区分大小写
print("20sdjhasj".isalpha()) #False
print("sdjhasj".isalpha()) #True
print(" sdjhasj".isalpha()) #False

#isdigit()
#每一个字符都是数字
print("123".isdigit()) #True

#isalnum()
#每一个字符都是数字或字母
print("123abc".isalnum()) #True
print("abc".isalnum()) #True
print("123".isalnum()) #True
print(" 123abc".isalnum()) #False

#isspace()
#每一个字符都是空白符
#空格 缩进 换行等不可见转义字符 空串为False
print("".isspace()) #False
print("\t\n\r".isspace()) #True
print("  ".isspace()) #True

#startswith(prefix, start = 0, end = len(x))
#判断一个字符串是否以某前缀开头 返回bool
print("2023-2-21".startswith("2"))#True
print("2023-2-21".startswith("2",2))#True
print("2023-2-21".startswith("2",1))#False
print("2023-2-21".startswith("2", 0, 0))#False

#endswith(prefix, start = 0, end = len(x))
#是否以某字符串结尾
print("2012-2.doc".endswith("doc")) #True

#in
#判断一个字符串是否被另一个包含
print("a" in "abc") #True

#not in
#判断一个字符串是否不被另一个包含
print("a" not in "abc") #False

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值