10个有用的Python字符串函数小结

作者:海拥 时间:2023-08-13 11:26:28 

前言

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

五、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

来源:https://juejin.cn/post/6995091089900175373

标签:Python,字符串函数
0
投稿

猜你喜欢

  • MySQL Create Database 创建数据库

    2011-09-10 16:04:51
  • RedHat下MySQL的基本使用方法分享

    2024-01-18 06:00:16
  • Python读取mat文件,并保存为pickle格式的方法

    2023-02-15 19:34:41
  • mysql递归函数with recursive的用法举例

    2024-01-16 22:37:22
  • 详解Python中break语句的用法

    2021-12-21 22:18:17
  • Mysql 1864 主从错误解决方法

    2024-01-17 05:01:35
  • 从客户端提升SQL Server数据库性能

    2009-03-06 14:27:00
  • 浅谈vue项目可以从哪些方面进行优化

    2024-05-09 15:19:50
  • Centos5.x下升级python到python2.7版本教程

    2023-04-15 13:50:07
  • asp.net结合aspnetpager使用SQL2005的存储过程分页

    2023-07-14 00:22:23
  • 网站浏览器兼容的底线

    2007-12-22 11:26:00
  • Python使用win32com.client的方法示例

    2021-03-22 14:32:48
  • 基于Python实现牛牛套圈小游戏的示例代码

    2022-04-04 03:50:23
  • python基础pandas的drop()用法示例详解

    2023-06-11 15:11:39
  • 详解Appium+Python之生成html测试报告

    2022-12-21 22:38:51
  • Python configparser模块配置文件过程解析

    2023-03-04 09:35:49
  • Go语言实现新春祝福二维码的生成

    2023-10-16 06:14:32
  • MySQL中表分区技术详细解析

    2024-01-21 01:01:11
  • Python中函数及默认参数的定义与调用操作实例分析

    2022-01-15 05:16:31
  • python根据给定文件返回文件名和扩展名的方法

    2023-09-04 03:41:07
  • asp之家 网络编程 m.aspxhome.com