python字符串处理函数大总结

str=’python string function’

生成字符串变量str=’python string function’

字符串长度获取:len(str)

例:print ‘%s length=%d’ % (str,len(str))

1.字母处理

立即学习“Python免费学习笔记(深入)”;

   全部大写:str.upper()

   全部小写:str.lower()

   大小写互换:str.swapcase()

   首字母大写,其余小写:str.capitalize()

   首字母大写:str.title()

   print ‘%s lower=%s’ % (str,str.lower())

   print ‘%s upper=%s’ % (str,str.upper())

   print ‘%s swapcase=%s’ % (str,str.swapcase())

   print ‘%s capitalize=%s’ % (str,str.capitalize())

   print ‘%s title=%s’ % (str,str.title())

2.格式化相关

   获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)    

   获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)

   获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)

   获取固定长度,右对齐,左边不足用0补齐

   print ‘%s ljust=%s’ % (str,str.ljust(20))

   print ‘%s rjust=%s’ % (str,str.rjust(20))

   print ‘%s center=%s’ % (str,str.center(20))

   print ‘%s zfill=%s’ % (str,str.zfill(20))

3.字符串搜索相关

   搜索指定字符串,没有返回-1:str.find(‘t’)

   指定起始位置搜索:str.find(‘t’,start)

   指定起始及结束位置搜索:str.find(‘t’,start,end)

   从右边开始查找:str.rfind(‘t’)

   搜索到多少个指定字符串:str.count(‘t’)

   上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1

   print ‘%s find nono=%d’ % (str,str.find(‘nono’))

   print ‘%s find t=%d’ % (str,str.find(‘t’))

   print ‘%s find t from %d=%d’ % (str,1,str.find(‘t’,1))

   print ‘%s find t from %d to %d=%d’ % (str,1,2,str.find(‘t’,1,2))

   #print ‘%s index nono ‘ % (str,str.index(‘nono’,1,2))

   print ‘%s rfind t=%d’ % (str,str.rfind(‘t’))

   print ‘%s count t=%d’ % (str,str.count(‘t’))

4.字符串替换相关

   替换old为new:str.replace(‘old’,’new’)

   替换指定次数的old为new:str.replace(‘old’,’new’,maxReplaceTimes)

   print ‘%s replace t to *=%s’ % (str,str.replace(‘t’, ‘*’))

   print ‘%s replace t to *=%s’ % (str,str.replace(‘t’, ‘*’,1))

5.字符串去空格及去指定字符

   去两边空格:str.strip()

   去左空格:str.lstrip()

   去右空格:str.rstrip()

   去两边字符串:str.strip(‘d’),相应的也有lstrip,rstrip

   str=’ python String function ‘

   print ‘%s strip=%s’ % (str,str.strip())

   str=’python string function’

   print ‘%s strip=%s’ % (str,str.strip(‘d’))

6.按指定字符分割字符串为数组:str.split(‘ ‘)

   默认按空格分隔

   str=’a b c de’

   print ‘%s strip=%s’ % (str,str.split())

   str=’a-b-c-de’

   print ‘%s strip=%s’ % (str,str.split(‘-‘))

7.字符串判断相关

   是否以start开头:str.startswith(‘start’)

   是否以end结尾:str.endswith(‘end’)

   是否全为字母或数字:str.isalnum()

   是否全字母:str.isalpha()

   是否全数字:str.isdigit()

   是否全小写:str.islower()

   是否全大写:str.isupper()

   str=’python string function’

   print ‘%s startwith t=%s’ % (str,str.startswith(‘t’))

   print ‘%s endwith d=%s’ % (str,str.endswith(‘d’))

   print ‘%s isalnum=%s’ % (str,str.isalnum())

   str=’pythonStringfunction’

   print ‘%s isalnum=%s’ % (str,str.isalnum())

   print ‘%s isalpha=%s’ % (str,str.isalpha())

   print ‘%s isupper=%s’ % (str,str.isupper())

   print ‘%s islower=%s’ % (str,str.islower())

   print ‘%s isdigit=%s’ % (str,str.isdigit())

   str=’3423′

   print ‘%s isdigit=%s’ % (str,str.isdigit())

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2283478.html

(0)
上一篇 2025年2月27日 20:10:27
下一篇 2025年2月25日 18:02:32

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • python获取网络时间和本地时间

    今天我们来看一下如何用python获取网络时间和本地时间,直接上代码吧,代码中都有注释。 python获取网络时间 获取网络时间 def getBeijinTime():     “””    获取北京时间     “””     try:…

    编程技术 2025年2月27日
    200
  • Python集合(set)类型的操作总结

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric differenc…

    编程技术 2025年2月27日
    100
  • python网络编程中常用到的函数​总结

    总结一下python网络编程中常用到的函数 socket.getservbyname(servicename[, protocolname]) –> integer   查询某个协议对应的端口号,需要使用两个参数,servicenam…

    编程技术 2025年2月27日
    200
  • python 序列化之JSON和pickle详解

    JSON模块 json(javascript object notation) 是一种轻量级的数据交换格式。它基于ecmascript的一个子集。 json采用完全独立于语言的文本格式,但是也使用了类似于c语言家族的习惯(包括c、c++、j…

    2025年2月27日
    200
  • Python3获取大量电影信息

    实验室这段时间要采集电影的信息,给出了一个很大的数据集,数据集包含了4000多个电影名,需要我写一个爬虫来爬取电影名对应的电影信息。  其实在实际运作中,根本就不需要爬虫,只需要一点简单的Python基础就可以了。  前置需求: Pytho…

    2025年2月27日 编程技术
    200
  • Python字符串

    如下学习python的字符串用法。 print(dir(str)) [‘__add__’, ‘__class__’, ‘__contains__’, ‘__…

    编程技术 2025年2月27日
    200
  • python三级菜单

    menu = {    ‘北京’:{        ‘海淀’:{            ‘五道口’:{                ‘soho’:{},                ‘网易’:{},                ‘go…

    编程技术 2025年2月27日
    200
  • python杂记

    os模块说明:python os模块包含普遍的操作系统功能 os.access(path, mode) # 检验权限模式 os.chdir(path) # 改变当前工作目录os.chflags(path, flags) # 设置路径的标记为…

    编程技术 2025年2月27日
    200
  • 详解Python的装饰器

    python中的装饰器是你进入python大门的一道坎,不管你跨不跨过去它都在那里。 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数。 def say_hello():     print…

    编程技术 2025年2月27日
    200
  • Python模块:logging

    很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug、info、…

    2025年2月27日
    100

发表回复

登录后才能评论