• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    10个有用的Python字符串函数小结
    POST TIME:2021-10-18 13:10

    前言

    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

    到此这篇关于10个有用的Python字符串函数小结的文章就介绍到这了,更多相关Python字符串函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • python入门字符串拼接\截取\转数字理解学习
    • python入门课程第五讲之序列和字符串
    • Python如何使用print()函数输出格式化字符串
    • 怎么处理Python分割字符串时有多个分隔符
    • Python基本数据类型之字符串str
    • Python数字/字符串补零操作实例代码
    • python如何正确的操作字符串
    • python字符串的多行输出的实例详解
    • Python的文本常量与字符串模板之string库
    • 关于Python中字符串的各种操作
    上一篇:Numpy数组的组合与分割实现的方法
    下一篇:Python OpenCV 针对图像细节的不同操作技巧
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信