网站功能列表,天津专业网站设计,手机版百度入口,电商网站如何做精细化运营字符串#xff08;str#xff09;
对于字符串的学习#xff0c;我整理了网上的一些资料#xff0c;希望可以帮助到各位#xff01;#xff01;#xff01;
概述 由多个字母#xff0c;数字#xff0c;特殊字符组成的有限序列 字符串的定义#xff1a;可以使用一对… 字符串str
对于字符串的学习我整理了网上的一些资料希望可以帮助到各位
概述 由多个字母数字特殊字符组成的有限序列 字符串的定义可以使用一对单引号或者双引号也可以一对三个单引号或者一对三个双引号定义字符串。 注意 没有单符号的数据类型 a a s1 hello, world!
s2 你好世界
print(s1, s2)
# 以三个双引号或单引号开头的字符串可以折行
s3
hello,
world!print(s3, end) print函数中的end 表示输出后不换行即将默认的结束符\n换行符更换为 空字符 创建字符串
#创建字符串
strapple
str1orange
print(type(str),type(str1))#class str class str#\转义字符作用:让一些符号失去原有的意义
str2 \李白\
str3 \原神\
print(str2, str3)
# 李白 原神#定义字符串的时候,单双引号可以互相嵌套
str4原神,启动
print(str4)
# 原神,启动拼接和重复(、*)
s1 hello world
print(s1) # hello world
s2 ! * 3
print(s2) # !!!
s1 s2 # s1 s1 s2
print(s1) # hello world!!!
s1 * 2 # s1 s1 * 2
print(s1) # hello world!!!hello world!!!
比较运算(ord、is、id) 字符串的比较运算比较的是字符串的内容 如果不清楚两个字符对应的编码到底是多少可以使用ord()函数来获得 s1 a whole new world
s2 hello world
print(s1 s2, s1 s2) # False True
print(s2 hello world) # True
print(s2 Hello world) # False
print(s2 ! Hello world) # True
s3 李白
print(ord(李), ord(白)) # 26446 30333
s4 秦始皇
print(ord(秦), ord(始), ord(皇)) # 31206 22987 30343
print(s3 s4, s3 s4) # False True如果用is来比较两个字符串它比较的是两个变量对应的字符串对象的内存地址 s1 str(123)
s2 str(123)
s3 s2
# 比较字符串的内容
print(id(s1))#2293238767536
print(id(s2))#2293238767584
print(id(s3))#2293238767584
print(s1 s2, s2 s3) # True True
# 比较字符串的内存地址
print(s1 is s2, s2 is s3) # False True
字符串特殊处理(r、f、b、u) 1、字符串前加 r r 的作用是去除转义字符 . 即如果 是“\n”那么表示一个反斜杠字符 一个字母 n 而不是表示换行了。 以 r 开头的字符常用于正则表达式对应着 re 模块。 2、字符串前加 f #以f开头表示在字符串内支持大括号内的python表达式
import time
name py小王子
t0 time.time()
# 这里设一些耗时操作用sleep模拟
time.sleep(10)
print(f{name} done in {time.time() - t0:.2f}s)
# py小王子 done in 10.00s 3、字符串前加 b b前缀表示后面字符串是bytes类型。网络编程中服务器和浏览器只认bytes类型数据。 4、字符串前加u 例u我是含有中文字符组成的字符串。 后面字符串以 Unicode 格式进行编码一般用在中文字符串前面防止因为源码储存格式问题导致再次使用时出现乱码。 成员运算(in、not in)
s1 hello, world
print(wo in s1) # True
s2 goodbye
print(s2 in s1) # False
循环遍历(range)
方法1、
s1 hello
for index in range(len(s1)):print(s1[index])
方法2、
s1 hello
for ch in s1:print(ch)
字符串的下标和切片 下标 : 也叫索引 , 表示第几个数据 在程序中 , 下标一般从 0 开始 , 可以通过下标获取指定位置的数据 str1welcome
print(str1[0]) #w
print(str1[3]) #c 切片 : 从字符串中复制一段指定的内容 , 生成一个新的字符串 str2 welcome to beijing切片的语法:
字符串[start:end:step]截取的字符串包含开始下标对应的字符串
start表示开始下标end表示结束下标
step表示步长print(str2[0:3]) # wel 包含start不包含end
print(str2[1:]) # elcome to beijing 若只设置了start表示从开始下标一直截取到最后
print(str2[:4]) # welc 若只设置了end表示从第一个字符开始一直截取到指定结束的位置
print(str2[1:4:2]) # ec
# print(str2[1:4:0]) # 在切片的时候,步长不能设置为0,ValueError: slice step cannot be zero
print(str2[::]) # welcome to beijing 若未设置开始和结束,表示复制字符串
print(str2[::-1]) # gnijieb ot emoclew 表示翻转字符串
print(str2[-9:-3]) # o beij start和end若都为负数,表示从右边开始数注意因为字符串是不可变类型所以不能通过索引运算修改字符串中的字符 获取长度和次数(len、count)
str4 原神,启动
# 获取字符串的长度len()
print(len(str4)) # 11# count()在整个字符串中查找子字符串出现的次数
str 电脑卡了,ss电脑呢?
print(str.count(电脑)) # 2
# 在指定区间内查找出现的次数
print(str.count(电脑, 5, 30)) # 1字符串查找(find、rfind、index)
#find()查找子串在字符串中第一次出现的位置,返回的是下标,若未找到返回-1ss3123asdfASDCXaZ8765sahbzcd6a79
print(ss3.find(a)) #3print(ss3.find(y)) #-1 未找到子串,返回-1#在指定区间内查找
print(ss3.find(a,5,20)) #12#rfind查找子串在字符串中最后一次出现的位置,返回的是下标,若未找到返回-1 print(ss3.rfind(a)) #25
print(ss3.rfind(y)) #-1#index()功能和find类似在字符串中未找到的时候,直接报错
print(ss3.index(d)) #5
#print(ss3.index(y))#ValueError:substringnotfound#max()min()依据ASCII码进行的获取print(max(ss3)) #z
print(min(ss3)) #1
大小写转换(upper、lower、title)
#2.字符串大小写转换upper()lower()
#upper()将字符串中的小写字母转换为大写
str1i Miss you Very Much!
print(str1.upper()) #I MISS YOU VERY MUCH!#lower()将字符串中的大写字母转化为小写
print(str1.lower()) #i miss you very much!#swapcase 将字符串中的大写转换为小写,将小写转换为大写]
print(str1.swapcase()) #I mISS YOU vERY mUCH!#title()将英文中每个单词的首字母转换为大写
str2i love you forever!
print(str2.title()) # I Love You Forever!
提取(strip)
# strip()去除字符串两边的指定字符(默认去除的是空格)
ss4 today is a nice day
ss5 ***today is a nice day****
print(ss4) # today is a nice day
print(ss4.strip()) # today is a nice day
print(ss5) # ***today is a nice day****
print(ss5.strip(*)) # today is a nice day
# lstrip只去除左边的指定字符(默认去除的是空格)
print(ss5.lstrip(*)) # today is a nice day****
# rstrip只去除右边的指定字符(默认去除的是空格)
print(ss5.rstrip(*)) # ***today is a nice day分割和合并(split、splitlines、join)
# split()以指定字符对字符串进行分割(默认是空格)
ss6 this is a string example.....wow!
print(ss6.split()) # 以空格进行分割[this,is,a,string,example.....wow!]
print(ss6.split(i)) # [th, s , s a str, ng example.....wow!]# splitlines()按照行切割
ss7 将进酒
君不见黄河之水天上来,奔流到海不复回.
君不见高堂明镜悲白发,************.print(ss7)
print(ss7.splitlines()) # [将进酒, 君不见黄河之水天上来,奔流到海不复回., 君不见高堂明镜悲白发,************.]# join以指定字符进行合并字符串
ss8 -
tuple1 (hello, every, body)
print(tuple1) # [将进酒, 君不见黄河之水天上来,奔流到海不复回., 君不见高堂明镜悲白发,************.]
print(ss8.join(tuple1)) # hello-every-body替换(replace)
# 替换
# replace()对字符串中的数据进行替换
ss9 第五人格启动第五人格真好玩
print(ss9)
print(ss9.replace(第五人格, ***)) # ***启动***真好玩
# 控制替换的字符的次数
print(ss9.replace(第五人格, ***, 1)) # ***启动第五人格真好玩判断(isupper、islower、isdigit、istitle、isalpha)
# 字符串判断
# isupper()检测字符串中的字母是否全部大写
print(ASDqwe123.isupper()) # False
print(ASD123.isupper()) # True
print()# islower()检测字符串中的字母是否全部小写
print(ASDqwe123.islower()) # False
print(qwe123.islower()) # True
print()# isdigit()检测字符串是否只由数字组成
print(1234.isdigit()) # True
print(1234asd.isdigit()) # False
print()# istitle()检测字符串中的首字母是否大写
print(Hello World.istitle()) # True
print(hello every body.istitle()) # False
print()# isalpha()检测字符串是否只由字母和文字组成
print(你好everyone.isalpha()) # True
print(你好everyone123.isalpha()) # False
前缀和后缀(startswith、endwith)
# 前缀和后缀:判断字符串是否以指定字符开头或者以指定字符结束# startswith()判断字符串是否以指定字符开头
# endwith()判断字符串是否以指定字符结束
s1 HelloPython
print(s1.startswith(Hello)) # True
print(s1.endswith(thon)) # True编解码(encode、decode)
# encode()编码
# decode()解码
s2 hello py小王子
print(s2.encode()) # bhello py\xe5\xb0\x8f\xe7\x8e\x8b\xe5\xad\x90
print(s2.encode(utf-8)) # bhello py\xe5\xb0\x8f\xe7\x8e\x8b\xe5\xad\x90
print(s2.encode(gbk)) # bhello py\xd0\xa1\xcd\xf5\xd7\xd3
# 解码
s3 bhello py\xe5\xb0\x8f\xe7\x8e\x8b\xe5\xad\x90
print(s3.decode()) # hello py小王子ASCII码转换 chr() 将对应的ASCII码的值转换为对应的字符 ord() 获取对应字符的ASCII的值 print(chr(68)) # D
print(ord(a)) # 97 格式化输出(%) 通过%来改变后面字母或者数字的含义%被称为占位符 # 字符串格式化输出%占位符
%d表示整数
%f表示小数
%s表示字符串
%.3f(表示保留3位小数,保留的小数数位自己可以控制)name py小王子
sex 男
money 198987932.787532
print(我的姓名是:%s % name) # 我的姓名是:py小王子
print(我的大号是%s,性别是%s,我的财富是%.2f % (name, sex, money))
# 我的大号是py小王子,性别是男,我的财富是198987932.79
# 还可以通过f{}{}这种方式实现格式化输出
print(f我的大号是:{name},性别是:{sex},我的财富是{money}) # 我的大号是:py小王子,性别是:男,我的财富是198987932.787532格式化字符串(center、ljust、rjust) 在Python中字符串类型可以通过center、ljust、rjust方法做居中、左对齐和右对齐的处理。如果要在字符串的左侧补零也可以使用zfill方法。 s1 hello, world
print(wo in s1) # True
s2 goodbye
print(s2 in s1) # Falses hello, world
# center方法以宽度20将字符串居中并在两侧填充*
print(s.center(20, *)) # ****hello, world****
# rjust方法以宽度20将字符串右对齐并在左侧填充空格
print(s.rjust(20)) # hello, world
# ljust方法以宽度20将字符串左对齐并在右侧填充~
print(s.ljust(20, ~)) # hello, world~~~~~~~~
# 在字符串的左侧补零
print(33.zfill(5)) # 00033
print(-33.zfill(5)) # -0033总结
Python中操作字符串可以用拼接、切片等运算符也可以使用字符串类型的方法。 恭喜你学会了字符串快去试试吧