当前位置: 首页 > news >正文

python字符串的讲解和应用

目录

一、字符串基础操作

二、字符串去除空白与指定字符

三、字符串拼接与列表转换

四、字符串格式化(三种方式)

五、格式化进阶(对齐、精度、进制等)

六、字符串编码与解码

七、字符串验证(判断字符类型)

八、字符串去重


一、字符串基础操作

# 1. 定义字符串变量并打印原始字符串
s1 = 'Hello World'
print(s1)  # 输出: Hello World# 2. 使用lower()方法将字符串全部转换为小写字母并打印
new_str_1 = s1.lower()  # 注意:字符串方法返回新字符串,非列表
print(new_str_1)  # 输出: hello world# 3. 使用upper()方法将字符串全部转换为大写字母并打印
new_str_2 = s1.upper()
print(new_str_2)  # 输出: HELLO WORLD# 4. 使用split()方法按空格分割字符串为列表,并打印分割后的前两个元素
new_list_3 = s1.split(' ')  # 按空格分割为列表
print(new_list_3[0], new_list_3[1])  # 输出: Hello World# 5. 使用count()方法统计字符'o'在字符串中出现的次数并打印
print(s1.count('o'))  # 输出: 2(字符串中含两个'o')# 6. 使用find()方法查找字符'o'在字符串中首次出现的索引位置并打印
print(s1.find('o'))  # 输出: 4(索引从0开始)# 7. 使用find()方法查找字符'p'在字符串中的位置,不存在则返回-1
print(s1.find('p'))  # 输出: -1(字符串中无'p')# 8. 使用startswith()方法检查字符串是否以'Hello'开头并打印结果
print(s1.startswith('Hello'))  # 输出: True# 9. 使用endswith()方法检查字符串是否以'd'结尾并打印结果
print(s1.endswith('d'))  # 输出: True(字符串以'd'结尾)# 10. 使用replace()方法替换指定字符(仅替换1次)
new_str_4 = s1.replace('o', 'p', 1)  # 只替换第一个'o'
print(new_str_4)  # 输出: Hellp World# 11. 使用center()方法将字符串居中,并用'*'填充两侧至长度20
print(s1.center(20, '*'))  # 输出: ****Hello World*****

二、字符串去除空白与指定字符

# 12. 定义包含前后空白和前缀的字符串
s = '    dl-Hello World     '# 13. 使用strip()方法去除字符串两侧的空白字符
print(s.strip())  # 输出: dl-Hello World(去除两侧空格)# 14. 使用lstrip()方法去除字符串左侧的空白字符
print(s.lstrip())  # 输出: dl-Hello World     (仅去除左侧空格)# 15. 使用rstrip()方法去除字符串右侧的空白字符
print(s.rstrip())  # 输出:     dl-Hello World(仅去除右侧空格)# 16. 使用strip()方法去除字符串两侧指定的字符序列(空格、'd'、'l')
print(s.strip('    dl'))  # 输出: -Hello World(去除左侧空格、'd'、'l'和右侧空格)

三、字符串拼接与列表转换

# 17. 使用join()方法将列表元素用空格连接为字符串
words = ["Hello", "World", "!"]
result = " ".join(words)
print(result)  # 输出: Hello World !# 18. 错误示例:join()只能连接字符串,包含整数会报错
items = ["a", 1, "b"]
# print("".join(items))  # 报错:TypeError(元素1为整数)# 19. 正确做法:先将非字符串元素转换为字符串
print("".join(str(i) for i in items))  # 输出: a1b(整数1转为字符串)# 20. 多种字符串拼接方式
s1 = 'Hello'
s2 = 'World'
# (1)使用+拼接
print(s1 + s2)  # 输出: HelloWorld
# (2)使用join()拼接列表元素
print(''.join([s1, s2]))  # 输出: HelloWorld
# (3)使用join()拼接并添加分隔符
print('*'.join(['hello', 'world', 'java']))  # 输出: hello*world*java
# (4)直接用逗号拼接(默认空格分隔)
print(s1, s2)  # 输出: Hello World
# (5)使用%格式化拼接
print("%s%s" % (s1, s2))  # 输出: HelloWorld
# (6)使用f-string拼接
print(f'{s1}{s2}')  # 输出: HelloWorld
# (7)使用format()方法拼接
print('{0}{1}'.format(s1, s2))  # 输出: HelloWorld

四、字符串格式化(三种方式)

# 21. 定义格式化所需变量
name = '马冬梅'
age = 18
score = 97.5# (1)%占位符格式化(类似C语言)
# 常用占位符:%s(字符串)、%d(整数)、%f(浮点数)、%%(显示%)
print('姓名:%s,年龄:%d,成绩:%.1f' % (name, age, score))  # 输出: 姓名:马冬梅,年龄:18,成绩:97.5# (2)f-string格式化(Python 3.6+,简洁高效)
print(f'姓名:{name},年龄:{age},成绩:{score}')  # 输出: 姓名:马冬梅,年龄:18,成绩:97.5# (3)format()方法格式化(支持位置索引)
print('姓名:{0},年龄:{1},成绩:{2}'.format(name, age, score))  # 输出: 姓名:马冬梅,年龄:18,成绩:97.5

五、格式化进阶(对齐、精度、进制等)

# 22. 定义基础字符串
s = 'hello world'# (1)字符串对齐与填充(宽度20,用*填充)
print('{0:*<20}'.format(s))  # 输出: hello world*********(左对齐)
print('{0:*>20}'.format(s))  # 输出: *********hello world(右对齐)
print('{0:*^20}'.format(s))  # 输出: ****hello world*****(居中对齐,同center())# (2)千位分隔符(适用于整数和浮点数)
print('{0:,}'.format(12345678))  # 输出: 12,345,678
print('{0:,}'.format(12345678.987456))  # 输出: 12,345,678.987456# (3)浮点数精度控制(四舍五入)
print('{0:.2f}'.format(12.456789))  # 输出: 12.46(保留2位小数)# (4)字符串最大显示长度(截取前5个字符)
print('{0:.5}'.format(s))  # 输出: hello# (5)整数的多进制显示
a = 425
print('二进制:{0:b},十进制:{0:d},八进制:{0:o},十六进制:{0:x}。'.format(a))
# 输出: 二进制:110101001,十进制:425,八进制:651,十六进制:1a9。# (6)浮点数的多种格式
b = 3.1415926
print('{0:.2f},{0:.2E},{0:.2%}'.format(b))
# 输出: 3.14,3.14E+00,314.16%(保留2位小数、科学计数法、百分比)

六、字符串编码与解码

# 23. 字符串编码(str → bytes)
# 格式:     str.encode(encoding='编码格式', errors='错误处理方式')
#           encoding:指定编码格式(默认 utf-8)      errors:编码错误时的处理方式(strict 报错、ignore 忽略、replace 替换为 �)。
s = '伟大的中国梦'
# (1)默认以utf-8编码(中文占3字节)
scode_utf8 = s.encode(errors='ignore')  # errors='ignore'忽略编码错误
print(scode_utf8)  # 输出: b'\xe4\xbc\x9f\xe5\xa4\xa7\xe7\x9a\x84\xe4\xb8\xad\xe5\x9b\xbd\xe6\xa2\xa6'# (2)以gbk编码(中文占2字节)
scode_gbk = s.encode(encoding='gbk', errors='replace')  # errors='replace'替换错误字符
print(scode_gbk)  # 输出: b'\xce\xb0\xb4\xf3\xb5\xc4\xd6\xd0\xb9\xfa\xc3\xce'# 24. 字节解码(bytes → str)
#格式:    bytes.decode(encoding='编码格式', errors='错误处理方式')
#         encoding:指定编码格式(默认 utf-8)      errors:编码错误时的处理方式(strict 报错、ignore 忽略、replace 替换为 �)。
# (1)utf-8编码的字节解码为字符串
print(scode_utf8.decode('utf-8'))  # 输出: 伟大的中国梦
# (2)gbk编码的字节解码为字符串
print(scode_gbk.decode('gbk'))  # 输出: 伟大的中国梦
编码格式特点适用场景中文占用字节
UTF-8Unicode 标准的可变长度编码,兼容 ASCII全球通用(网页、文件、跨平台交互)1-4 字节(通常 3 字节)
GBK中国国家标准,兼容 GB2312,支持简体中文中文 Windows 系统、传统中文文档1-2 字节(中文 2 字节)
GB2312早期中文编码,仅支持 6763 个简体汉字老旧系统或特定历史文档1-2 字节(中文 2 字节)
ASCII仅支持英文字母、数字和部分符号纯英文场景(如早期协议)1 字节(不支持中文)
Unicode固定长度(通常 2 或 4 字节),包含全球所有字符内部处理(非存储 / 传输)2 或 4 字节

七、字符串验证(判断字符类型)

# 25. 数字验证
print('123'.isdigit())      # 输出: True(仅识别阿拉伯数字)
print('一二三'.isdigit())    # 输出: False(汉字数字不识别)
print('123'.isnumeric())    # 输出: True(识别所有数字类型,包括汉字数字)# 26. 字母验证
print('hello你好'.isalpha())   # 输出: True(所有字符均为字母/汉字)
print('hello你好13'.isalpha()) # 输出: False(包含数字13)# 27. 字母+数字验证
print('hello你好一二三'.isalnum())  # 输出: True(仅包含字母、汉字、数字)# 28. 大小写验证
print('helloWord'.islower())  # 输出: False(存在大写字母W)
print('helloword'.islower())  # 输出: True(全小写)
print('helloWord'.isupper())  # 输出: False(存在小写字母)
print('HELLOWORD'.isupper())  # 输出: True(全大写)# 29. 首字母大写验证(title格式)
print('HelloWord'.istitle())  # 输出: False(Word的W大写,不符合title格式)
print('Hello Word'.istitle()) # 输出: True(每个单词首字母大写)# 30. 空白字符验证
print(' '.isspace())   # 输出: True(空格是空白字符)
print('\n\t'.isspace()) # 输出: True(换行\n、制表符\t均为空白字符)

八、字符串去重

# 31. 方法1:遍历字符串去重(保留首次出现顺序)
s = 'hello world hello word java python c mysql spring apple hello'
new_s1 = ''
for char in s:if char not in new_s1:new_s1 += char
print(new_s1)  # 输出: helo wrdjavpytncmsqig# 32. 方法2:通过索引遍历去重(与方法1效果相同)
new_s2 = ''
for i in range(len(s)):if s[i] not in new_s2:new_s2 += s[i]
print(new_s2)  # 输出: helo wrdjavpytncmsqig# 33. 方法3:用集合去重后排序(保留首次出现顺序)
new_set = set(s)  # 集合去重(无序)
new_list = list(new_set)
new_list.sort(key=s.index)  # 按原字符串中首次出现的索引排序
new_s3 = ''.join(new_list)
print(new_s3)  # 输出: helo wrdjavpytncmsqig

http://www.dtcms.com/a/289705.html

相关文章:

  • kotlin Flow快速学习2025
  • Function Callingの进化路:源起篇
  • (5)从零开发 Chrome 插件:Vue3 Chrome 插件待办事项应用
  • 7.20 树hash |字典树模板❗
  • LangChain4j多模型共存+整合SpringBoot
  • springboot websocket 自动重启方案
  • SpringBoot3集成MapstructPlus
  • 抓包工具使用教程
  • 网安-文件上传-upload-labs
  • Laravel 原子锁概念讲解
  • jdk各个版本特性
  • Linux 基础文件IO操作
  • 零基础学习性能测试第一章:核心性能指标-并发量
  • Node.js 中基于请求 ID 实现简单队列(即时阻止策略/排队等待策略)
  • DMZ网络
  • (1)Windows环境下安装Oracle
  • Vue3 Proxy 数据劫持为什么比Object.defineProperty() Vue2数据劫持的性能更好
  • 人工智能训练师三级实操题第一部分数据处理
  • shell 脚本基础学习
  • Java中的intern()方法
  • 全新安装Proxmox VE启动时卡在Loading initial ramdisk
  • RAII机制以及在ROS的NodeHandler中的实现
  • 【c++】200*200 01灰度矩阵求所有的连通区域坐标集合
  • 鸿蒙开发中 渲染范围的控制
  • 飞腾D2000的BIOS编译
  • 在服务器无网络的环境下安装 VS Code Remote-SSH 组件
  • 【Python练习】053. 编写一个函数,实现简单的文件加密和解密功能
  • C++string类(3)
  • 基于单片机的火灾报警系统设计
  • SaTokenException: 未能获取对应StpLogic 问题解决