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

python中string模块各属性以及函数的用法介绍

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

jingxian 通过本文主要向大家介绍了python string模块,python中string模块,python3 string模块,python fromstring,python list string等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作。

python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求:

• python的字符串属性函数

• python的string模块

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.字符串属性函数 

系统版本:CentOS release 6.2 (Final)2.6.32-220.el6.x86_64

python版本:Python 2.6.6

字符串属性方法

字符串格式输出对齐

>>> str='stRINg lEArn' 
 
>>>
 
>>> str.center(20)   #生成20个字符长度,str排中间
 
'  stRINg lEArn  '
 
>>> 
 
>>> str.ljust(20)    #str左对齐
 
'stRINg lEArn    ' 
 
>>>
 
>>> str.rjust(20)    #str右对齐
 
'    stRINg lEArn'
 
>>> 
 
>>> str.zfill(20)    #str右对齐,左边填充0
 
'00000000stRINg lEArn'
</div>

大小写转换

>>> str='stRINg lEArn'  
 
>>> 
 
>>> str.upper() #转大写
 
'STRING LEARN'
 
>>> 
 
>>> str.lower() #转小写
 
'string learn'
 
>>> 
 
>>> str.capitalize() #字符串首为大写,其余小写
 
'String learn'
 
>>> 
 
>>> str.swapcase() #大小写对换
 
'STrinG LeaRN'
 
>>> 
 
>>> str.title() #以分隔符为标记,首字符为大写,其余为小写
 
'String Learn'
</div>

字符串条件判断

>>> str='0123' 
 
>>> str.isalnum() #是否全是字母和数字,并至少有一个字符 
True 
 
>>> str.isdigit() #是否全是数字,并至少有一个字符 
True 
 
 
 
>>> str='abcd' 
 
>>> str.isalnum() 
 
True 
 
>>> str.isalpha() #是否全是字母,并至少有一个字符 
True 
 
>>> str.islower() #是否全是小写,当全是小写和数字一起时候,也判断为True 
True 
 
 
 
>>> str='abcd0123' 
 
>>> str.islower() #同上 
 
True 
 
>>> str.isalnum()  
 
True 
 
 
 
>>> str=' ' 
 
>>> str.isspace() #是否全是空白字符,并至少有一个字符 
True 
 
>>> str='ABC' 
 
>>> str.isupper() #是否全是大写,当全是大写和数字一起时候,也判断为True 
True 
 
>>> str='Abb Acc' 
 
>>> str.istitle() #所有单词字首都是大写,标题 
True 
 
 
>>> str='string learn' 
>>> str.startswith('str') #判断字符串以'str'开头 
True 
>>> str.endswith('arn') #判读字符串以'arn'结尾 
True
</div> </div>

字符串搜索定位与替换

>>> str='string lEARn' 
 
>>> 
 
>>> str.find('a')  #查找字符串,没有则返回-1,有则返回查到到第一个匹配的索引 
 
-1 
 
>>> str.find('n') 
 
4 
 
>>> str.rfind('n')  #同上,只是返回的索引是最后一次匹配的 
 
11 
 
>>> 
 
>>> str.index('a')  #如果没有匹配则报错 
 
Traceback (most recent call last): 
 
 File "<stdin>", line 1, in <module> 
 
ValueError: substring not found 
 
>>> str.index('n')  #同find类似,返回第一次匹配的索引值 
 
4 
 
>>> str.rindex('n') #返回最后一次匹配的索引值 
 
11 
 
>>> 
 
>>> str.count('a')  #字符串中匹配的次数 
 
0 
 
>>> str.count('n')  #同上 
 
2 
 
>>> 
 
>>> str.replace('EAR','ear') #匹配替换 
 
'string learn' 
 
>>> str.replace('n','N') 
 
'striNg lEARN' 
 
>>> str.replace('n','N',1) 
 
'striNg lEARn' 
 
>>> 
 
>>> 
 
>>> str.strip('n') #删除字符串首尾匹配的字符,通常用于默认删除回车符 
 
'string lEAR' 
 
>>> str.lstrip('n') #左匹配 
 
'string lEARn' 
 
>>> str.rstrip('n') #右匹配 
 
'string lEAR' 
 
>>> 
 
>>> str=' tab' 
 
>>> str.expandtabs() #把制表符转为空格 
 
'  tab' 
 
>>> str.expandtabs(2) #指定空格数 
 
' tab' 
</div> </div>

字符串编码与解码

>>> str='字符串学习' 
 
>>> str 
 
'xe5xadx97xe7xacxa6xe4xb8xb2xe5xadxa6xe4xb9xa0' 
 
>>> 
 
>>> str.decode('utf-8')    #解码过程,将utf-8解码为unicode 
 
u'u5b57u7b26u4e32u5b66u4e60' 
 
 
 
>>> str.decode('utf-8').encode('gbk') #编码过程,将unicode编码为gbk 
 
'xd7xd6xb7xfbxb4xaexd1xa7xcfxb0' 
 
>>> str.decode('utf-8').encode('utf-8') #将unicode编码为utf-8 
 
'xe5xadx97xe7xacxa6xe4xb8xb2xe5xadxa6xe4xb9xa0'
</div> </div>

字符串分割变换

>>> str='Learn string' 
 
>>> '-'.join(str) 
 
'L-e-a-r-n- -s-t-r-i-n-g' 
 
>>> l1=['Learn','string'] 
 
>>> '-'.join(l1) 
 
'Learn-string' 
 
>>> 
 
>>> str.split('n') 
 
['Lear', ' stri', 'g'] 
 
>>> str.split('n',1) 
 
['Lear', ' string'] 
 
>>> str.rsplit('n',1) 
 
['Learn stri', 'g'] 
 
>>> 
 
>>> str.splitlines() 
 
['Learn string'] 
 
>>> 
 
>>> str.partition('n') 
 
('Lear', 'n', ' string') 
 
>>> str.rpartition('n') 
 
('Learn stri', 'n', 'g')
</div> </div>

string模块源代码

"""A collection of string operations (most are no longer used). 
 
 
Warning: most of the code you see here isn't normally used nowadays.
 
Beginning with Python 1.6, many of these functions are implemented as
 
methods on the standard string object. They used to be implemented by
 
a built-in module called strop, but strop is now obsolete itself.
 
 
Public module variables:
 
 
whitespace -- a string containing all characters considered whitespace
 
lowercase -- a string containing all characters considered lowercase letters
 
uppercase -- a string containing all characters considered uppercase letters
 
letters -- a string containing all characters considered letters
 
digits -- a string containing all characters considered decimal digits
 
hexdigits -- a string containing all characters considered hexadecimal digits
 
octdigits -- a string containing all characters considered octal digits
 
punctuation -- a string containing all characters considered punctuation
 
printable -- a string containing all characters considered printable
 
 
"""
 
 
# Some strings for ctype-style character classification
 
whitespace = ' tnrvf'
 
lowercase = 'abcdefghijklmnopqrstuvwxyz'
 
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 
letters = lowercase + uppercase
 
ascii_lowercase = lowe

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

  • python中string模块各属性以及函数的用法介绍

相关文章

  • Python设计模式之代理模式实例
  • python自动翻译实现方法
  • 小小聊天室Python代码实现
  • Python线程中对join方法的运用的教程
  • Python中MySQL数据迁移到MongoDB脚本的方法
  • Python常用的内置序列结构(列表、元组、字典)学习笔记
  • python基于pyDes库实现des加密的方法
  • python网络编程之数据传输UDP实例分析
  • Python Tkinter简单布局实例教程
  • tensorflow实现在函数中用tf.Print输出中间值

文章分类

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

最近更新的内容

    • python共享引用(多个变量引用)示例代码
    • 在Python的Flask框架下收发电子邮件的教程
    • Python selenium 父子、兄弟、相邻节点定位方式详解
    • python压缩文件夹内所有文件为zip文件的方法
    • python实现问号表达式(?)的方法
    • python将图片文件转换成base64编码的方法
    • 简单介绍Python下自己编写web框架的一些要点
    • Python类的基础入门知识
    • Python编写检测数据库SA用户的方法
    • Python中dictionary items()系列函数的用法实例

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

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