曹旭辉 发表于 7 天前

[Python学习日志-17] 细讲数据类型——字符串

细讲数据类型——字符串
简介
字符串的增查改
字符串的常用方法

简介

        字符串在编程当中非经常用,比方在登岸功能中,用户输入的用户名、密码、验证码等都是利用字符串的形式来存储的,在 Python 中,字符串是一种表示文本数据的数据类型。字符串可以是由任意字符组成的,用单引号或双引号括起来即可。
定义:字符串是一个有序的字符的集合,用于存储和表示根本的文本信息,' ' 或 " " 中间包含的内容称之为字符串
字符串的特点:
1.Python 中的字符串是一种有序的,按照从左到右的次序定义字符集合,下标从0开始次序访问
https://i-blog.csdnimg.cn/direct/0dac9fafe90e4e548c65664bbec2cd2b.jpeg
2.可以进行切片操纵
3.Python 中的字符串是不可变的,不能像列表一样修改此中某个元素,全部对字符串的修改操纵其实都是相称于生成了一份新数据
留意:
        字符串的单引号和双引号都无法取消特殊字符的寄义,如果想让引号内全部字符均取消特殊意义,在引号前面加 r,如 name = r'i\thf',如果不加 r 在引号前面特殊字符将会被转译
https://i-blog.csdnimg.cn/direct/29a70156e40149d28ef97374b9134233.png
        除了在引号前面加 r 外还有一个方法是可以消除特殊字符转译的,那就是在特殊字符前加“\”,其着实引号前面加 r 也是执行了同样的操纵,只不过是 Python 帮你自动加上了
https://i-blog.csdnimg.cn/direct/b19c51a2ae534f058c926121a9dc387c.png

字符串的增查改

一、增

str = "Hello,Jove! How are you?" https://i-blog.csdnimg.cn/direct/f1a86bb2c24d4f4180fd06133478e53f.png
二、查

        字符串其实也可以把它理解为一个列表大概时元组,由于他们身上相似的地方套多了,比方它们都可以通过下标查找,也可以进行切片
str = "Jove"

# 普通查询
print(str)
print(str,str,str,str)
print(str[-4],str[-3],str[-2],str[-1])

# 切片,与列表相同同样是顾头不顾尾
print(str)
print(str[-4:-1])

# 步长
print(str)
print(str[-4:-1:3])
print(str[::-1])

# 除以上的方法外还有 find(),index(),count()... 详细请看字符串的常用操作小节  https://i-blog.csdnimg.cn/direct/f3b622e3cd83497b82cc0d9a8312105d.png
三、改

        从查当中可以看出,字符串和列表一样是可以每个字符每个字符如许查找的,不过对于字符串的修改却不能逐个字符如许修改,它可以整个字符串进行修改大概先对字符串进行切片,然后插入需要修改的字符,最后进行拼接
# 整个进行修改
str = "Jove"
print(id(str),str)
str = "Kerry"
print(id(str),str)

# 结合切片和拼接进行修改特定字符
# 例如需要把 Jove 中的 o 修改为a
str = "Jove"
print(id(str),str)
str1 = str
str2 = str
str = str1 + "a" + str2
print(id(str),str)

# 除以上的方法外还有 replace(),upper(),lower(),swapcase(),casefold(),strip(),split()... 详细请看字符串的常用操作小节 https://i-blog.csdnimg.cn/direct/f66e922183a44a979c6e809c0f0e159f.png
        从现象可以看出,无论是通过整体的修改大概是切片再拼接的方法进行修改都是会从新生成一个新的内存地点 

字符串的常用方法

        字符串操纵方法有非常多,但是并不是全部都会用到,本篇就只讲一些重要的方法,至于其他方法如果有爱好的话可以利用 help(str) 来研究研究
一、 首字母大写

s = "hello world"
print(s.capitalize())  https://i-blog.csdnimg.cn/direct/077b581af9d5488199d63eb5f280f63e.png
二、字母全小写

s = "HELLO,WORLD"
print(s.casefold())  https://i-blog.csdnimg.cn/direct/b17a2bec9b94417399c1dc31c43a4eb1.png
三、 长度填充

s = "hello,world"
print(s.center(50, '-'))    # 总长度,填充字符  https://i-blog.csdnimg.cn/direct/770669519c58438d91de81b41c71a3af.png
四、统计指定字符数目

s = "hello,world"
print(s.count('o', 2, 5))    # 查找字符,开始标号,结束标号;顾头不顾尾  https://i-blog.csdnimg.cn/direct/5d919d0a8bd644b4bff4b5e2751834d3.png
五、字符串编码

        现在还没讲到编码,后续学习到编码会再讲 
s = "hello,world"
print(s.encode('utf-8'))    # 字符串 s 进行 utf-8 编码 https://i-blog.csdnimg.cn/direct/18dc49fec4b34c52b5ec505f0255d3db.png
六、判定结尾

s = "hello,world"
print(s.endswith('WORLD'))
print(s.endswith('world'))  https://i-blog.csdnimg.cn/direct/fa53e9f6f06e415c8aaf8cae0060b078.png
七、在字符串中查找字符

s = "hello,world"
# 有则返回下标,无则返回-1,与 index() 相同,但 index() 无则报错
print(s.find('o'))
# 查找字符,开始标号,结束标号;顾头不顾尾
print(s.find('O', 5, 9))
print(s.index('o', 5, 9))
print(s.index('O', 5, 9))  https://i-blog.csdnimg.cn/direct/c9ce28f9c58945a59b4125b04c0aaef5.png
八、代位

s = "Welcome %s to Apeland,you are No.%s user." % ("Jack", 888)
print(s)

s = "Welcome {0} to Apeland,you are No.{1} user,{0}hahaha."
print(s.format("Eva", 999))

name = "Lucy"
number = 777
s = "Welcome {name} to Apeland,you are No.{number} user."
print(s.format(name=name, number=number))  https://i-blog.csdnimg.cn/direct/aec363b813704723bcddce5d37d42e83.png
九、代位(利用字典)

        现在还没讲到字典,后续学习到字典会再讲
mapping = {"name":"Lucy","number":"777"}
s = "Welcome {name} to Apeland,you are No.{number} user."
print(s.format_map(mapping))  https://i-blog.csdnimg.cn/direct/d8e64e6844e548cd9a7da96242152411.png
十、判定字符串是否整数

# 可用于数据类型转换前的判断
student_num = "32"
print(student_num.isdigit())

if student_num:
    num = int(student_num)
    print(num, type(num))  https://i-blog.csdnimg.cn/direct/15a50420aa9e460286a670bf85fd9992.png
十一、判定字符串当中是否满是小写

lower = "abcdefg"
print(lower, lower.islower())
lower += "G"
print(lower, lower.islower())  https://i-blog.csdnimg.cn/direct/0162daa938c04360872bbdfcdc80a914.png
十二、字符串是否为空格

print("\033  https://i-blog.csdnimg.cn/direct/111d601e38fc4712ad74a73224d45b88.png
十三、拼接字符串

n = ['1', '2', '3', '4', '5']
print(" ".join(n))    # 列表中的元素必须是字符串,否则无法拼接  https://i-blog.csdnimg.cn/direct/4dc25bd4252142988c57d25d345aa800.png
十四、从左边开始数填充字符串长度(填充在右边)

lower = "abcdefg"
print(lower.ljust(50, "*"))    # 从左开始数,不够的位数填充 * https://i-blog.csdnimg.cn/direct/cf158043b5904345a05b231778ec6206.png
十五、从右边开始数填充字符串长度(填充在左边)

lower = "abcdefg"
print(lower.rjust(50, "*"))    # 从右开始数,不够的位数填充 *  https://i-blog.csdnimg.cn/direct/8a94b9e51ba944d796ad396452df89d1.png
十六、把字符串当中的大写都转成小写

s = "HELLO,WORLD"
print(s.lower())  https://i-blog.csdnimg.cn/direct/ea1537767b9b412e8090740198323b0e.png
十七、把除字符外的其他功能性的特殊字符去掉

ss = "    \thello,\nworld   \n"
print(ss.strip())    # 但在字符串中的空格与特殊字符无法去除
print(ss.lstrip())    # lstrip()只把左边除字符外的其他功能性的特殊字符去掉
print(ss.rstrip())    # rstrip()只把右边除字符外的其他功能性的特殊字符去掉  https://i-blog.csdnimg.cn/direct/4ce5ebcda58741aabb2b9a2d11a09527.png
十八、批量修改指定字符

# 默认所有指定的字符都进行修改,但也可以规定只改几次
my_score = "My score is 580,not very good.Of coure,mu honey score as 580,too."
print(my_score.replace("580", "650", 1))    # 从左到右数改1次
print(my_score.replace("580", "650"))    # 全都改 https://i-blog.csdnimg.cn/direct/1c361af590054167b8acd451dd511f61.png
十九、把字符串转换为列表

# 默认会以空格作为分割符
apple = ["iPhone", "iPad", "iPad Pro", "iPod", "iWatch", "iPhone"]
apple2 = ",".join(apple)
print(apple2)
print(apple2.split())    # 默认为空格
print(apple2.split(","))  https://i-blog.csdnimg.cn/direct/e4e90d906ae94735a89eb29daf81484a.png
二十、按次数分割字符串为列表

apple2 = 'iPhone,iPad,iPad Pro,iPod,iWatch,iPhone'
# 从0开始数起,也可以说是列表的长度
print(apple2.split(",", 2))    # 分割符号,分割几次
print(apple2.rsplit(",", 1))    # 分割符号,分割几次,从右边的分隔符开始分  https://i-blog.csdnimg.cn/direct/2f829f23e591474ab3872623a889bfe5.jpeg
二十一、判定字符串开头字符

apple2 = 'iPhone,iPad,iPad Pro,iPod,iWatch,iPhone'
print(apple2.startswith("i"))
print(apple2.startswith("i", 1))    # 从字符串标号为1的字符开始  https://i-blog.csdnimg.cn/direct/b1361556098749679a241dc37f9ff815.jpeg
二十二、巨细写对换

apple2 = 'iPhone,iPad,iPad Pro,iPod,iWatch,iPhone'
print(apple2.swapcase())  https://i-blog.csdnimg.cn/direct/6bee0e2934784854b42008599a8ae0fe.jpeg
二十三、把字符串全酿成大写

s = "hello,world"
print(s.upper()) https://i-blog.csdnimg.cn/direct/329892adc1344b7194cb9fa278cb04a0.jpeg
二十四、用0填充在字符串的左边

s = "hello,world"
print(s.zfill(100)) https://i-blog.csdnimg.cn/direct/29fec294c0a34492a1eee061c4d0217b.jpeg



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: [Python学习日志-17] 细讲数据类型——字符串