目录
右耳零基础Python--字符编码,解码_m0_59882269的博客-CSDN博客
5.1find() ,rfind(),index(),rindex(),count() 查
5.2split(),rsplit(),partition(),rpartition() 分割
5.4lower(),upper(),capitalize(),title(),swapcase()
5.8isdigit((),isspace(),isupper(),islower(),isalpha,isalnum
本章学习目标
- 了解ASCII ,UTF_8,GBK 等常见字符编码格式
- 了解转义字符和原始字符的概念和用法
- 掌握字符串格式化方法format()的用法
- 熟练运用字符串常用方法
- 熟练运用运算符和内置函数对字符串的操作
1.字符串概述
字符串是指包含若干字符的容器结构。在python中字符串属于不可变有序序列。使用单引号,双引号,三单引号,三双引号作为定界符,并且不同定界符之间可以相互嵌套。
''HelloWorld''
'这个字符串是数''12''和字''abc''的组合'
'''你好!我是xxx'''
2.字符串编码格式
右耳零基础Python--字符编码,解码_m0_59882269的博客-CSDN博客
注意:中文与英文字符同样对待,都算一个字符
3.转义字符与原始字符串
转义字符是指:在字符串中某些特定的符号前加一个斜线之后,该字符被解释成另一种含义,不在表示原来的字符。
\b 退格,把光标移动到前一列位置
\f 换页符
\n 换行符
\t 制表符
\\ 普通的一个斜杠
\' 普通的单引号
\'' 普通的双引号
\ooo 3位八机制数对应的字符
\xhh 2位16进制数对应的字符
\uhhhh 4位16进制数表示的Unicode字符
原始字符串:为了避免对原始字符串进行转义,可以使用原始字符串。指的是在字符串前面加上字母r或者R表示原始字符串,其中所有字符都表示原始的含义我、而不会被进行任何转义。
path='C:\Windows\nptepad.exe' print(path) path=r'C:\Windows\nptepad.exe' print(path)
C:\Windows
ptepad.exe
C:\Windows\nptepad.exe
4.字符串格式化
name=input("请输入你的名字:")
address=input("请输入住址:")
age=int(input("请输入你的年龄"))
hobby=input("请输入你的爱好")
# %s字符占位,他是万能占位符
# %d占位整数
# s="我叫%s,我住在%s,我今年多少%d岁,我喜欢做%s"%(name,address,age,hobby)
# print(s)
# s1="我叫{},我住在{},我今年{}岁,我喜欢{}".format(name,address,age,hobby)
# print(s1)
s2=f"我叫{name},我住在{address},我今年{age},我喜欢{hobby}"# f-string
print(s2)
以上是字符串格式化常用方法,下面对他进行详解
4.1使用%符号进行格式化
要求被格式化的内容和格式字符之间的数量和顺序都必须是一一对应的
%[-][+][0][m][.n] 格式字符%x
格式运算符%之前的部分为格式字符串,之后的部分为需要进行格式化内容
%格式标志,表示格式开始
[-]指定左对齐输出
[+]对整数加正号
[0]指定空位填0
[m]指定最小宽度
[.n]指定精度
格式符号 指定类型
%格式运算符
x 待转换的表达式
python支持大量的格式字符
%s格式化为字符串
%d十进制整数
%o八进制整数
%x十六进制整数
%f浮点数
%%格式化为%的一个符号
a=12345 c="%o" %a print(c) a=12345 c="%e" %a print(c)
30071
1.234500e+04
4.2使用format()方法进行字符串格式化
不要求格式化内容和格式字符之间的顺序严格一致
a="{0:.3f}".format(1/3) print(a) a="{0:%}".format(1/3) print(a) a="the number {0:,} in hex is:{0:#x},in oct is {0:#o}".format(55) print(a) a="the number {0:,} in hex is:{0:#x},in oct is {0:#o},the number {1:,} in hex is:{1:#x},in oct is {1:#o}".format(55,155) print(a)
0.333
33.333333%
the number 55 in hex is:0x37,in oct is 0o67
the number 55 in hex is:0x37,in oct is 0o67,the number 155 in hex is:0x9b,in oct is 0o233
4.3格式化字符串常量
Formatted String Literals 其中大括号里面的表示占位符
address="河南" a=f"my address is {address}" print(a)
my address is 河南
5.字符串常用方法
5.1find() ,rfind(),index(),rindex(),count() 查
s="zxcvbnagcdghs" a=s.find("c")#前面 print(a) a=s.find("c",5) print(a) a=s.rfind("c")#后面 print(a) a=s.index("c") print(a) a=s.rindex("c") print(a)
2
8
8
2
8
注意:find找不到会返回-1,index找不到是报错
a=s.count("c") print(a)#2
5.2split(),rsplit(),partition(),rpartition() 分割
s="2021-10-25" s=s.split("-") print(s) print(list(map(int,s)))
['2021', '10', '25']
[2021, 10, 25]
特别注意:partition()是将原字符分成三部分,即分隔符之前的字符串,分隔符字符串,分隔符之后的字符串
s="apple,banana,pear,peach" s=s.partition(",") print(s)
('apple', ',', 'banana,pear,peach')
s="apple,banana,pear,peach" s=s.partition("t") print(s)
('apple,banana,pear,peach', '', '')没有分隔符的情况
5.3join()
join()方法是用来将列表中的多个字符串进行拼接,并在两个相邻的字符串之间插入指定字符,返回新的字符串。
li=["pear","apple"] p="-".join(li) print(p)
pear-apple
5.4lower(),upper(),capitalize(),title(),swapcase()
s="absbh dh dAAAj" print(s.lower()) print(s.upper()) print(s.title())#每个单词首字母大写 print(s.capitalize())#字符串首字母大写 print(s.swapcase())#大小写转化
absbh dh daaaj
ABSBH DH DAAAJ
Absbh Dh Daaaj
Absbh dh daaaj
ABSBH DH DaaaJ
5.5replace()字符串的替换
s="absbh dh dAAAj" s=s.replace("ab","jiayou") print(s)
jiayousbh dh dAAAj
5.6strip()去除两端分隔符
s=" absbh dh dAAAj " s=s.strip() print(s)
absbh dh dAAAj
5.7startwith(),endwith()
s="absbh dh dAAAj" print(s.startswith("a"))# True print(s.endswith("a"))#False
判断字符串以什么开头以什么结尾
5.8isdigit((),isspace(),isupper(),islower(),isalpha,isalnum
s="123" print(s.isdigit())#判断字符串是否为整数 print(s.isalnum())#判断字符串是字母,数字 print(s.isalpha())#判断字符串是字母 print(s.isspace())#判断字符串是否全部是空白符 print(s.isupper())#判断是否是全部大写字母 print(s.islower())#判断是否全部是小写字母
True
True
False
False
False
False
5.9center(),ljust(),rjust()
这几个方法用于对字符串进行排版,center居中,ljust左对齐,rjust右对齐
s="Main Menu".center(20)#默认以空格进行填充 print(s) s="Main Menu".ljust(20,"#")#左对齐,右侧警号填充 print(s) s="Main Menu".rjust(20,"-")#右对齐,左侧-填充 print(s)
Main Menu
Main Menu###########
-----------Main Menu
6.字符串支持的运算符
连接运算符+
测试运算符in
序列重复 乘法
s="hello"+"world" print(s) s="helloworld"*3 print(s) s="helloworld" s="hello"in s print(s)
helloworld
helloworldhelloworldhelloworld
True
7字符串切片
''Explicit is better than implicit''.[:8]
"Explicit"
8.字符串常量
python标准库string提供了英文字母大小写,数字字符,标点符号等常量,可以直接使用
''' 使用sting模块提供的字符串常量,模拟生成指定长度的随机密码 思路:把英文字母大小写,数字作为字符集,用choice从中随机选择n个字符使用join把生成的字符连接成一个字符串。 ''' from random import choice from string import ascii_letters,digits character=digits+ascii_letters def generatePassWorld(n): return "".join((choice(character) for _ in range(n))) print(generatePassWorld(8)) print(generatePassWorld(15))
lRumr8yK
G3cS7YLwD7ZSTb2