• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python内置的字符串处理函数整理

Python内置的字符串处理函数整理

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 字符串函数,python字符串长度函数,字符串替换函数python,python字符串反转函数,python字符串处理函数等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

str='python String function'

生成字符串变量str='python String function'

字符串长度获取:len(str)
例:print '%s length=%d' % (str,len(str))

字母处理
全部大写: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())
格式化相关
获取固定长度,右对齐,左边不够用空格补齐: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))

字符串搜索相关
搜索指定字符串,没有返回-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'))

字符串替换相关
替换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))

字符串去空格及去指定字符
去两边空格: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'))

按指定字符分割字符串为数组: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('-'))

字符串判断相关
是否以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())

还有其他常见的Python字符串处理 函数的话不定期更新。

</div>

您可能想查找下面的文章:

  • python中根据字符串调用函数的实现方法
  • Python中常用操作字符串的函数与方法总结
  • python中常用检测字符串相关函数汇总
  • Python字符串和文件操作常用函数分析
  • Python字符串和文件操作常用函数分析
  • Python内置的字符串处理函数详细整理(覆盖日常所用)
  • Python内置的字符串处理函数详细整理(覆盖日常所用)
  • 浅析python 内置字符串处理函数的使用方法
  • Python内置的字符串处理函数整理
  • Python内置的字符串处理函数整理

相关文章

  • 在Python的Django框架中为代码添加注释的方法
  • python搭建简易服务器分析与实现
  • python实现的一只从百度开始不断搜索的小爬虫
  • python实现多线程抓取知乎用户
  • Python和perl实现批量对目录下电子书文件重命名的代码分享
  • 简单使用Python自动生成文章
  • Python实现的tab文件操作类分享
  • 非递归的输出1-N的全排列实例(推荐)
  • 基于wxpython开发的简单gui计算器实例
  • 学习python类方法与对象方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python中的赋值、浅拷贝、深拷贝介绍
    • 通过mod_python配置运行在Apache上的Django框架
    • django实现分页的方法
    • Python深入学习之上下文管理器
    • 详解使用python crontab设置linux定时任务
    • python获取list下标及其值的简单方法
    • linux环境下安装pyramid和新建项目的步骤
    • 在Python中使用第三方模块的教程
    • 各个系统下的Python解释器相关安装方法
    • python每次处理固定个数的字符的方法总结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有