百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

全面掌握 Python 字符串

itomcoil 2025-02-09 12:37 17 浏览


本文专注于 Python 字符串总结,试图一文全面讲解 Python 常用字符串操作,并且能够一遍就能看懂和学会。

文本目标

零散的 Python 字符串知识是没有力量的。文本的目标你脑子里面长出一颗 Python 字符串的树的种子,让它具有成长的基础属性。当然发芽成长还是自己。

学习方式推荐


推荐使用学习方式使用 jupyter notebook,为什么呢?很简单 jupyter notebook 可以分段执行,也可以分类。当然你可以使用自己的

pip install jupyterlab

jupyter lab # 在 web 环境中链接

当然你也可以在 vscode 使用插件支持 jupyter notebook,在你 vscode 等熟悉的编程器中学习和使用。

定义字符串的方式


# 1. 单引号字符串
str1 = '这是一个字符串'

# 2. 双引号字符串
str2 = "这是另一个字符串"

# 3. 三引号字符串(可以跨多行)
str3 = '''这是一个
多行字符串
可以换行'''

# 4. 原始字符串(反斜杠不转义)
str4 = r'这是一个原始字符串,包含反斜杠: \n \t'


# 5. Unicode 字符串(Python 3 中,字符串默认是 Unicode)
str5 = u'这是一个Unicode字符串'

# 6. 格式化字符串(插入变量或表达式的值)
name = 'Alice'
age = 30
str6 = f'{name}的年龄是{age}岁'

# 7. 字节字符串(用于存储字节数据)
str7 = b'hello world'

字符串的基本操作

# 原始字符串定义
str1 = "Hello"
str2 = "World"

# 1. 获取长度:len()
length = len(str1)

# 2. 类型转换:str()
num = 123
str_num = str(num)

# 3. 字符串连接:+
str3 = str1 + " " + str2

# 4. 重复字符串:*
str4 = str1 * 3

# 5. 索引和切片: str[i] 和 [str[i:j]]
first_char = str1[0]  # 索引获取第一个字符
slice_str = str1[1:4]  # 切片获取子字符串,从索引 1 到 3(不包含 4)

# 6. 字符串遍历: for-in
print("遍历字符串结果:")
for char in str1:
    print(char, end=' ')

字符串查找

# 原始字符串定义
text = "Hello, welcome to the world of Python. Python is great!"

# 查找子字符串:find()
substring = "Python"
index = text.find(substring)

# 子串计数:count() 方法返回子字符串在字符串中出现的次数
count = text.count(substring)

# 检查开头和结尾:startswith() 方法检查字符串是否以指定的子字符串开始
starts = text.startswith("Hello")

# endswith() 方法检查字符串是否以指定的子字符串结束
ends = text.endswith("great!")

# endswith() 方法可以接受一个元组,检查字符串是否以元组中的任意子字符串结尾
ends_any = text.endswith(("Python", "great!"))

字符串修改



# 原始字符串定义
text = "   Hello, welcome to the world of Python. Python is great!   "

# 1. 大小写转换:lower() 和 upper()
lower_text = text.lower()  # 将字符串转换为小写
upper_text = text.upper()  # 将字符串转换为大写

# 2. 替换:replace()
replaced_text = text.replace("Python", "Java")

# 3. 去空格:strip(), lstrip(), rstrip()
stripped_text = text.strip()    # 去除两端空格
lstripped_text = text.lstrip()  # 去除左侧空格
rstripped_text = text.rstrip()  # 去除右侧空格

# 4. 字符串拆分:split()
words = text.split()  # 默认按空白字符拆分

# 5. 字符串连接:join()
separator = ", "
joined_text = separator.join(words)  # 用 ', ' 连接列表中的字符串

格式化字符串:


name = "Alice"
age = 30

# 使用 % 占位符进行格式化
formatted_string = "名字: %s, 年龄: %d" % (name, age)

formatted_string = "名字: {}, 年龄: {}".format(name, age)
formatted_string_index = "名字: {0}, 年龄: {1}".format(name, age)
formatted_string_keywords = "名字: {name}, 年龄: {age}".format(name=name, age=age)

# 使用 f-string 进行格式化
formatted_string = f"名字: {name}, 年龄: {age}"

# 支持表达式
formatted_string_expr = f"名字: {name.upper()}, 年龄: {age + 5}"

# 支持格式化规范
formatted_string_float = f"圆周率: {3.14159:.2f}"

字符串检查


text = "  Hello123  "

print(f"'{text}' 是否全是字母或数字: {text.isalnum()}")  # 输出: False
print(f"'{text}' 是否全是字母: {text.isalpha()}")  # 输出: False
print(f"'{text}' 是否全是数字: {text.isdigit()}")  # 输出: False
print(f"'{text}' 是否全是空格: {text.isspace()}")  # 输出: False
print(f"'{text}' 是否全是小写字母: {text.islower()}")  # 输出: False
print(f"'{text}' 是否全是大写字母: {text.isupper()}")  # 输出: False

正则 re 模块

import re

# 正则表达式模式
pattern = r'\d+'  # 匹配一个或多个数字

# 1. 编译正则表达式:compile()
compiled_pattern = re.compile(pattern)

# 2. 查找:search()
text = "The number is 12345."
match_search = compiled_pattern.search(text)

# 3. 匹配:match()
text2 = "12345 is the number."
match_match = compiled_pattern.match(text2)

# 4. 匹配成列表:findall()
text3 = "The numbers are 123 and 456."
matches_findall = compiled_pattern.findall(text3)

# 5. 查找迭代:finditer()
matches_finditer = compiled_pattern.finditer(text3)
for match in matches_finditer:
    print(f"位置: {match.start()}, 内容: {match.group()}")

# 6. 替换:sub()
text4 = "Replace 123 and 456."
replaced_text = compiled_pattern.sub('NUMBER', text4)

# 7. 拆分:split()
text5 = "Split 123 and 456."
split_text = compiled_pattern.split(text5)

常用的类库


import string
import textwrap
import difflib
import unicodedata
from fuzzywuzzy import fuzz
import regex
import stringcase
from strsimpy.jaccard import Jaccard
from pyparsing import Word, alphas, nums, OneOrMore

# 1. string 内置模块的属性和方法:
print("所有字母: ", string.ascii_letters)
print("所有小写字母: ", string.ascii_lowercase)
print("所有大写字母: ", string.ascii_uppercase)
print("所有数字: ", string.digits)
print("所有标点符号: ", string.punctuation)
print("所有空白字符: ", string.whitespace)
template = string.Template('Hello, $name!')
print(template.substitute(name='Alice'))  # 输出: Hello, Alice!

# 2. textwrap 模块
text = "This is a long line of text that we want to wrap to a specified width for better readability."
wrapped_text = textwrap.fill(text, width=40)
print("textwrap.fill() 结果:")
print(wrapped_text)

# 3. difflib 模块
text1 = "Hello World!"
text2 = "Hello Python World!"
diff = difflib.ndiff(text1, text2)
print("difflib.ndiff() 结果:")
print(''.join(diff))

# 4. unicodedata 模块
char = '?'
print(f"字符: {char}")
print(f"名称: {unicodedata.name(char)}")  # 输出: LATIN SMALL LETTER N WITH TILDE
print(f"类别: {unicodedata.category(char)}")  # 输出: Ll (Letter, lowercase)

# 5. pyparsing 模块

word = Word(alphas)
number = Word(nums)
sentence = OneOrMore(word | number)
result = sentence.parseString("Hello 123 world")
print("pyparsing 结果:")
print(result)  # 输出: ['Hello', '123', 'world']

# 6. fuzzywuzzy 模块
str1 = "hello world"
str2 = "hello"
ratio = fuzz.ratio(str1, str2)
print(f"fuzzywuzzy.ratio() 相似度: {ratio}")  # 输出: 相似度: 60

# 7. regex 模块

text = "The quick brown fox jumps over the lazy dog."
pattern = r'\b\w{5}\b'
matches = regex.findall(pattern, text)
print(f"regex.findall() 匹配的单词: {matches}")  # 输出: ['quick', 'brown', 'jumps']

# 8. stringcase 模块
import stringcase
text = "Hello World"
print(f"stringcase.snakecase() 下划线风格: {stringcase.snakecase(text)}")  # 输出: hello_world
print(f"stringcase.uppercase() 大写风格: {stringcase.uppercase(text)}")  # 输出: HELLO WORLD
print(f"stringcase.camelcase() 驼峰风格: {stringcase.camelcase(text)}")  # 输出: HelloWorld

# 9. strsimpy 模块
jaccard = Jaccard(2)  # 2 表示使用2个字符组成的元组进行比较
similarity = jaccard.similarity("night", "nacht")
print(similarity)  # 输出: 0.14285714285714285

小结

本文系统以图文的方式介绍的了 Python 字符串相关内容和实际示例。熟悉一门语言我们快速的建立起知识结构,尤其是当你有多门编程语言的经验。

相关推荐

selenium(WEB自动化工具)

定义解释Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7,8,9,10,11),MozillaF...

开发利器丨如何使用ELK设计微服务中的日志收集方案?

【摘要】微服务各个组件的相关实践会涉及到工具,本文将会介绍微服务日常开发的一些利器,这些工具帮助我们构建更加健壮的微服务系统,并帮助排查解决微服务系统中的问题与性能瓶颈等。我们将重点介绍微服务架构中...

高并发系统设计:应对每秒数万QPS的架构策略

当面试官问及"如何应对每秒几万QPS(QueriesPerSecond)"时,大概率是想知道你对高并发系统设计的理解有多少。本文将深入探讨从基础设施到应用层面的解决方案。01、理解...

2025 年每个 JavaScript 开发者都应该了解的功能

大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发。1.Iteratorhelpers开发者...

JavaScript Array 对象

Array对象Array对象用于在变量中存储多个值:varcars=["Saab","Volvo","BMW"];第一个数组元素的索引值为0,第二个索引值为1,以此类推。更多有...

Gemini 2.5编程全球霸榜,谷歌重回AI王座,神秘模型曝光,奥特曼迎战

刚刚,Gemini2.5Pro编程登顶,6美元性价比碾压Claude3.7Sonnet。不仅如此,谷歌还暗藏着更强的编程模型Dragontail,这次是要彻底翻盘了。谷歌,彻底打了一场漂亮的翻...

动力节点最新JavaScript教程(高级篇),深入学习JavaScript

JavaScript是一种运行在浏览器中的解释型编程语言,它的解释器被称为JavaScript引擎,是浏览器的一部分,JavaScript广泛用于浏览器客户端编程,通常JavaScript脚本是通过嵌...

一文看懂Kiro,其 Spec工作流秒杀Cursor,可移植至Claude Code

当Cursor的“即兴编程”开始拖累项目质量,AWS新晋IDEKiro以Spec工作流打出“先规范后编码”的系统工程思维:需求-设计-任务三件套一次生成,文档与代码同步落地,复杂项目不...

「晚安·好梦」努力只能及格,拼命才能优秀

欢迎光临,浏览之前点击上面的音乐放松一下心情吧!喜欢的话给小编一个关注呀!Effortscanonlypass,anddesperatelycanbeexcellent.努力只能及格...

JavaScript 中 some 与 every 方法的区别是什么?

大家好,很高兴又见面了,我是姜茶的编程笔记,我们一起学习前端相关领域技术,共同进步,也欢迎大家关注、点赞、收藏、转发,您的支持是我不断创作的动力在JavaScript中,Array.protot...

10个高效的Python爬虫框架,你用过几个?

小型爬虫需求,requests库+bs4库就能解决;大型爬虫数据,尤其涉及异步抓取、内容管理及后续扩展等功能时,就需要用到爬虫框架了。下面介绍了10个爬虫框架,大家可以学习使用!1.Scrapysc...

12个高效的Python爬虫框架,你用过几个?

实现爬虫技术的编程环境有很多种,Java、Python、C++等都可以用来爬虫。但很多人选择Python来写爬虫,为什么呢?因为Python确实很适合做爬虫,丰富的第三方库十分强大,简单几行代码便可实...

pip3 install pyspider报错问题解决

运行如下命令报错:>>>pip3installpyspider观察上面的报错问题,需要安装pycurl。是到这个网址:http://www.lfd.uci.edu/~gohlke...

PySpider框架的使用

PysiderPysider是一个国人用Python编写的、带有强大的WebUI的网络爬虫系统,它支持多种数据库、任务监控、项目管理、结果查看、URL去重等强大的功能。安装pip3inst...

「机器学习」神经网络的激活函数、并通过python实现激活函数

神经网络的激活函数、并通过python实现whatis激活函数感知机的网络结构如下:左图中,偏置b没有被画出来,如果要表示出b,可以像右图那样做。用数学式来表示感知机:上面这个数学式子可以被改写:...