10 个有用的 Python 字符串函数

简介: 10 个有用的 Python 字符串函数

👓 前言

Python 字符串是一个内置的类型序列。字符串可用于处理 Python 中的文本数据。Python 字符串是 Unicode 点的不可变序列。在 Python 中创建字符串是最简单易用的。要在 Python 中创建字符串,我们只需将文本括在单引号和双引号中。Python 对单引号和双引号语句的处理方式相同。因此,在本文中,我们将讨论 Python 中用于数据分析和数据操作的一些重要且有用的字符串函数,主要用于自然语言处理(NLP)。

我们将在本文中讨论的 Python 字符串函数如下:

😊 一、capitalize() 函数

capitalize() 函数返回一个字符串,其中第一个字符是大写。作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

语法:string.capitalize()

示例 1:使给定句子中的第一个字母大写

string = "CSDN is the largest developer community in China" 
print(string.capitalize())

输出:

Csdn is the largest developer community in china

示例 2:如果第一个字符是数字而不是字符会发生什么

string = '3th CSDN force plan activities are very good' 
print(string.capitalize())

输出:

3th csdn force plan activities are very good

💌 二、lower( ) 函数

lower() 函数返回一个字符串,其中给定字符串中的所有字符都是小写。这个函数对符号和数字没有任何作用,即,只是忽略了这些东西。

语法:string.lower()

示例 1:小写给定字符串

string = "Haiyong is an excellent CSDN blogger" 
print(string.lower())

输出:

haiyong is an excellent csdn blogger

示例 2: 如果有数字而不是字符会发生什么

string = '3th CSDN force plan activities are very good' 
print(string.lower())

输出:

3th csdn force plan activities are very good

⏰ 三、title( ) 函数

title() 函数返回一个字符串,其中字符串中每个单词的第一个字符都是大写。它就像标题或标题。

如果字符串中的任何单词包含数字或符号,则此函数将其后的第一个字母转换为大写。

语法:string.title()

示例 1:使每个单词的第一个字母大写

string = "The blog you are reading will be on the hot list" 
print(string.title())

输出:

The Blog You Are Reading Will Be On The Hot List

示例 2:如果有数字而不是字符会发生什么

string = '10 useful Python string functions you must know' 
print(string.title())

输出:

10 Useful Python String Functions You Must Know

🧿 四、casefold() 函数

casefold() 函数返回一个字符串,其中所有字符都是小写。

这个函数类似于lower()函数,但是casefold()函数更强大,更激进,这意味着它将更多的字符转换成小写,并且在比较两个字符串时会找到更多的匹配项,并且都使用casefold()进行转换 功能。

语法:string.casefold()

示例 1:将给定的字符串变为小写

string = "CSDN is the largest developer community in China" 
print(string.casefold())

输出:

csdn is the largest developer community in china

示例 2:如果有数字而不是字符会发生什么

string = '10 useful Python string functions you must know' 
print(string.casefold())

输出:

10 useful python string functions you must know
10 useful python string functions you must know

🏀 五、upper( ) 函数

upper() 函数返回一个字符串,其中给定字符串中的所有字符都为大写。这个函数对符号和数字没有任何作用,即,只是忽略了这些东西。

语法:string.upper()

示例 1:给定字符串的大写

string = "CSDN is the largest developer community in China" 
print(string.upper())

输出:

CSDN IS THE LARGEST DEVELOPER COMMUNITY IN CHINA

示例 2:如果有数字而不是字符会发生什么

string = '10 useful Python string functions you must know' 
print(string.upper())

输出:

10 USEFUL PYTHON STRING FUNCTIONS YOU MUST KNOW



🏆 六、count( ) 函数

count() 函数查找指定值(由用户给定)在给定字符串中出现的次数。

语法:  string .count( value, start, end )

示例 1:返回值“CSDN ”在字符串中出现的次数

string = "CSDN is the largest developer community in China" 
print(string.count("CSDN "))

输出:

1

示例 2: 返回值“CSDN ”在字符串中 从位置 8 到 16 出现的次数

string = "CSDN is the largest developer community in China" 
print(string.count("analytics", 8, 16))

输出:

0

🎻 七、find( ) 函数

find() 函数查找指定值的第一次出现。如果在该字符串中找不到该值,则返回 -1。

find() 函数与 index() 函数几乎相同,但唯一的区别是 index() 函数在找不到值时引发异常。

语法:string.find(value, start, end)

示例 1:文本中字母“d”第一次出现的位置是什么?

string = "CSDN is the largest developer community in China" 
print(string.find("d"))

输出:

20

示例 2:仅在位置 5 和 16 之间搜索时,字母“d”在文本中的哪个位置首次出现?

string = "CSDN is the largest developer community in China" 
print(string.find("d", 12, 22))

输出:

20

示例 3:如果找不到该值,则 find() 函数返回 -1,但 index() 函数会引发异常

string = "CSDN is the largest developer community in China" 
print(string.find("d", 5, 10))

输出:

-1

🎥 八、replace() 函数

replace() 函数用另一个指定的短语替换指定的短语。

注意:如果没有指定任何其他内容,所有出现的指定短语都将被替换。

语法:  string .replace( oldvalue, newvalue, count )

示例 1:替换所有出现的单词“developer ”

string = "CSDN is the largest developer community in China" 
print(string.replace("largest ", "best "))

输出:

CSDN is the best developer community in China

示例 2:仅替换第一次出现的单词“developer ”

string = "CSDN is China's largest developer community suitabsle for developer to learn" 
print(string.replace("developer ", "developers ", 1))

输出:

CSDN is China's largest developers community suitabsle for developer to learn

🍖 九、swapcase( ) 函数

swapcase() 函数返回一个字符串,其中所有大写字母都是小写字母,反之亦然。

语法:string.swapcase()

示例 1:将小写字母变为大写,将大写字母变为小写

string = "CSDN is the largest developer community in China" 
print(string.swapcase())

输出:

csdn IS THE LARGEST DEVELOPER COMMUNITY IN cHINA

示例 2:如果有数字而不是字符会发生什么

string = '3th CSDN force plan activities are very good' 
print(string.swapcase())

输出:

3TH csdn FORCE PLAN ACTIVITIES ARE VERY GOOD

✨ 十、join () 函数

join() 函数获取可迭代对象中的所有项并将它们连接成一个字符串。我们必须指定一个字符串作为分隔符。

语法:string.join(iterable)

示例 1:将给定元组中的所有项连接成一个字符串,使用 #(hashtag)字符作为分隔符

myTuple = ("Computer Scientist", "Programming Learning", "Python Programming") 
x = " # ".join(myTuple) 
print(x)

输出:

Computer Scientist # Programming Learning # Python Programming

示例2:将给定字典中的所有项目连接成一个字符串,使用单词“TEST”作为分隔符

myDict = {"name": "CSDN", "country": "China", "Technology": "Python Programming"} 
mySeparator = "TEST" 
x = mySeparator.join(myDict) 
print(x)

输出:

nameTESTcountryTESTTechnology



😊 结尾想说的

我希望你喜欢这篇文章。如果你喜欢它,也分享给你的朋友。有未提及的内容或想分享您的想法请随时在下面发表评论,我会尽快回复您。😉


目录
相关文章
|
11天前
|
数据采集 Python
python学习9-字符串
python学习9-字符串
|
11月前
|
存储 Python
【Python入门篇】——Python基础语法(字符串格式化,表达式格式化和数据输入)
【Python入门篇】——Python基础语法(字符串格式化,表达式格式化和数据输入)
110 0
|
索引 Python
python基础—字符串操作
(1)字符串: Python内置了一系列的数据类型,其中最主要的内置类型是数值类型、文本序列(字符串)类型、序列(列表、元组和range)类型、集合类型、映射(字典)类型。本章在介绍字符串、列表、元组和range类型共有的通用序列操作方法的基础上,详细讲解字符串类型的创建、表示、字符串遍历、字符串操作、字符串处理方法和格式化等内容。并以random库的一些应用为例讲授字符串的实际应用,同时进一步加强流程控制语句的相关知识的应用训练。
|
Python
python知识点100篇系列(1)-字符串格式化的几种方式
python知识点100篇系列(1)-字符串格式化的几种方式
101 0
python知识点100篇系列(1)-字符串格式化的几种方式
|
Python
python字符串处理方式-python学习笔记29
python字符串处理方式-python学习笔记29
41 0
|
存储 Python
上手python之变量和字符串
上手python之变量和字符串
上手python之变量和字符串
Python 编程 | 连载 05 - 字符串操作
Python 编程 | 连载 05 - 字符串操作
Python 编程 | 连载 05 - 字符串操作
|
SQL 移动开发 C语言
Python 字符串格式化全面学习|学习笔记
快速学习 Python 字符串格式化全面学习
151 0
|
SQL 移动开发 C语言
Python字符串格式化全面学习|学习笔记
快速学习Python字符串格式化全面学习
82 0
|
Python 容器
Python第一次笔记 数据类型 字符串 python三种循环 函数
Python第一次笔记 数据类型 字符串 python三种循环 函数
97 0