(原创)Python字符串系列(1)——str对象
在本博客 《Python字符串系列》 中,将介绍以下内容:
本文将介绍Python内置的 str 类型,列举Python中字符串对象支持的方法,使用这些方法可以实现强大的字符串处理功能。
在Python 2 中,普通字符串与Unicode字符串有着明确的区分,二者都是Python内置的基本类型,例如:
>>> type(str)
<type 'type'>
>>> type(unicode)
<type 'type'>
str 类型的字符串通常被称为普通字符串(plain string),区别于 unicode 类型的字符串(unicode string):
>>> s = 'this is a plain string.'
>>> type(s)
<type 'str'>
>>> u = u'this is a unicode string.'
>>> type(u)
<type 'unicode'>
unicode字符串通常以 'u' 开头。
在Python 2中,使用 抽象基类 basestring 判断一个字符串是不是 str 类型或者 unicode 类型的实例,因为二者都是 basestring 的子类。
>>> issubclass(str, basestring)
True
>>> issubclass(unicode, basestring)
True
>>> issubclass(unicode, str)
False
本文将介绍Python字符串的内置方法,这些方法对于 plain string 和 unicode 字符串同样适用,如果执行操作的s是一个plain string,那么返回的字符串也是一个plain string,unicode类似。后续的文章将详细分析 unicode 字符串的特性及编码上的一些特点。
Python中,字符串是不可变的序列,支持:重复、连接、索引和切片等操作,例如:
>>> s * n # 将 s 重复n次
>>> s1 + s2 # 将字符串 s1 和 s2 连接
>>> s[0] # 索引字符串 s1 中的第一个字符
>>> s[0:2] # 返回字符串 s 中的前两个字符
这些操作都不会改变参与运算的字符串,除非进行显式地赋值。此外,Python还包括了许多字符串处理的小技巧,如:
- 使用s[::-1]可以翻转整个字符串
- 如果一个字符串全部由数字组成,而开头有0,则使用 int(s) 能够自动除去开头的0,将原来的字符串转成一个有意义的整数。
Python内置了丰富的字符串处理功能
1. 首字母大写
capitalize()
s.capitalize()
返回s的一份拷贝,并不改变s。如果 s 的首字符是一个字母,则拷贝的首字母将其改成大写,其余所有字母转成小写。
例如:
>>> 'this is a test string.'.capitalize()
'This is a test string.'
>>> '_this is a test string.'.capitalize()# 开头不是字母,不变
'_this is a test string.'
>>> 'this is A test string.'.capitalize()# 除开头外的其他位置上的字母全转成小写
'This is a test string.'
2. 对齐方式
(1)左右对齐 ljust()、rjust()
s.ljust(width[, fillchar])
s.rjust(width[, fillchar])
返回一个长度为 max(len(s), width) 的字符串,如果 width > len(s),则左/右对齐,并在另一端填充 fillchar
例如:
>>> '1234'.rjust(8, '#')
'####1234'
>>> '1234'.ljust(8, '#')
'1234####'
>>> '1234'.ljust(2, '#')
'1234'
(2)居中 center()
s.center(n, fillchar=' ')
返回一个新的字符串,新字符串的长度为 max(len(s), n),当 n > len(s)时,使用参数 fillchar (默认为空格)填充新字符串中其余的位置,并将 s 置于新字符串的中部。
例如:
>>> 'test'.center(3)
'test'
>>> 'test'.center(5)
' test'
>>> 'test'.center(6, '#')
'#test#'
>>> 'test'.center(7, '~')
'~~test~'
可见当左右无法均衡填充时,优先填充左侧。
3. 计数
count()
s.count(sub, start=0, end=sys.maxint)
统计 s[start:end] 中,子串 sub 出现的次数。
4. str 与 unicode 的转换
(1)str到unicode——decode()
S.decode([encoding[,errors]])
使用 decode() 函数可以将 str 类型的plain string 转换成 unicode 类型的字符串,
例如:
>>> s = '你好'
>>> u = s.decode('gbk')
>>> type(s)
<type 'str'>
>>> type(u)
<type 'unicode'>
>>> print s
你好
>>> print u
你好
>>> s
'\xc4\xe3\xba\xc3'
>>> u
u'\u4f60\u597d'
>>> len(s)
4
>>> len(u)
2
(2)Unicode 到 str——encode()
S.encode([encoding[,errors]])
使用encode()则可以将 unicode 字符串 转换成 str 类型的 plain string。
例如:
>>> u = u'你好'
>>> s = u.encode('gbk')
>>> type(u)
<type 'unicode'>
>>> type(s)
<type 'str'>
>>> u
u'\u4f60\u597d'
>>> s
'\xc4\xe3\xba\xc3'
>>> print u
你好
>>> print s
你好
>>> len(u)
2
>>> len(s)
4
5. 前后缀
endswith()、startswith()
S.endswith(suffix[, start[, end]])
s.startswith(prefix[, start[, end]])
返回 bool 型结果,
判断 s[start:end]是否以 suffix 结尾。
6. 扩展制表符
expandtabs()
S.expandtabs([tabsize])
tabsize默认为8,返回一个 s 的拷贝,其中的“\t”都被扩展成 tabsize 个空格。
例如:
>>> 'test\ttest'.expandtabs()
'test test'
7. 定位
(1)定位不到时返回 -1 find()、rfind()
s.find(sub [,start [,end]])
s.rfind(sub [,start [,end]])
返回 int 型结果,表示 sub 在 s[start:end] 中第一次出现的下标。如果在 s[start:end] 中没有找到子串 sub,则返回 -1。
例如:
>>> 'testtest'.find('est')
1
>>> 'testtest'.find('tt')
3
>>> 'testtest'.find('tt',3)
3
>>> 'testtest'.find('tt',4)
-1
(2)定位不到时抛出异常 index()、rindex()
S.index(sub [,start [,end]])
s.rindex(sub [,start [,end]])
功能与 find() 类似,但是如果没有找到 sub ,则抛出 ValueError。
例如:
>>> 'hello this world'.index('$')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
8.
format()
S.format(*args, **kwargs)
返回 字符串 型结果,
9. 内容形式判断
isalnum()、isalpha()、isdigit()、isspace()
s.isalnum()
返回布尔型结果,
判断 s 是不是非空,且全部由 字母 和 数字 组成。
s.isalpha()
返回布尔型结果,
判断 s 是不是非空,且全部由 字母 组成。
s.isdigit()
返回布尔型结果,
判断 s 是不是非空,且全部由 数字 字符组成。
例如:
>>> '123'.isdigit()
True
>>> '123.456'.isdigit()
False
s.isspace()
如果 len(s) > 0,且其中的所有字符都是空格,则返回True;
如果 s 为空,或s中存在至少一个非空格的字符,则返回False。
10. 大小写
(1)小写 islower()、lower()
s.islower()
s.lower()
返回布尔型结果,
如果 s 中不含一个小写字母,或至少含有一个大写字母,则返回False,否则返回True,包含其他字符并不影响。
例如:
>>> '123.456'.islower()
False
>>> 'abcde'.islower()
True
>>> 'abcde$'.islower()
True
>>> 'abcde#%^%'.islower()
True
>>> 'abcdeF'.islower()
False
>>> 'a.213214$#@^%$@'.islower()
True
(2)大写 isupper()、upper()
s.isupper()
s.upper()
如果 s 中包含的所有字母都是大写,则返回 True
如果s 中不包含字母,或者至少包含一个小写字母,则返回False。
例:
>>> 'ABC$@'.isupper()
True
>>> 'ASDFGq'.isupper()
False
>>> ''.isupper()
False
(3)交换大小写 swapcase()
s.swapcase()
11. "titlecase"
istitle()、title()
s.istitle()
s.title()
判断一个字符串是不是“titlecase”:每个独立的连续字母段都以大写字母开头。
例如:
>>> 'A Title'.istitle()
True
>>> 'a Title'.istitle()
False
>>> '123 this is a string'.istitle()
False
>>> 'This Is a String'.istitle()
False
12. 连接
join()
s.join(iterable)
以 s 为分隔符连接 iterable 中的字符串
例如:
>>> '$'.join(['hello','this','world'])
'hello$this$world'
13. 拆分
(1)保留分隔符的一次拆分 partition()、rpartition()
s.partition(sep)
s.rpartition(sep)
以 sep 为分隔符拆分 s ,返回 (head, sep, tail) 形式的三元组。
例如:
>>> 'hello$this$world'.partition('$')
('hello', '$', 'this$world')
>>> 'hello$this$world'.rpartition('$')
('hello$this', '$', 'world')
>>> 'hello this world'.partition('$')
('', '', 'hello this world')
(2)不保留分隔符的完全拆分 split()、rsplit()、splitlines()
s.split([chars])
s.rsplit([sep [,maxsplit]])
s.splitlines(keepends=False)
例如:
>>> 'hello$this$world'.split('$')
['hello', 'this', 'world']
14.
lstrip()、strip()、rstrip()
s.lstrip([chars]) #从开头删除
s.strip([chars]) # 左右两端同时删除
s.rstrip([chars]) # 从结尾删除
16. 替换
replace()
s.replace(old, new[, count])
18.
translate()
s.translate(table [,deletechars])
s.zfill(width)
(原创)Python字符串系列(1)——str对象的更多相关文章
- Python语言系列-08-面向对象3
反射 #!/usr/bin/env python3 # author: Alnk(李成果) # 反射 # hasattr getattr setattr delattr class Animal(ob ...
- Python坑系列:可变对象与不可变对象
在之前的文章 http://www.cnblogs.com/bitpeng/p/4748148.html 中,大家看到了ret.append(path) 和ret.append(path[:])的巨大 ...
- Python语言系列-07-面向对象2
重构父类__init__方法 #!/usr/bin/env python3 # author:Alnk(李成果) # 需求:Dog类要新增一个实例属性,但是Cat类不需要 class Animal(o ...
- Python语言系列-06-面向对象1
楔子 #!/usr/bin/env python3 # author:Alnk(李成果) # 人狗大战例子引入面向对象 # 版本1 def hero(name, sex, hp, ce, level= ...
- python字符串系列--2
#!/usr/bin/python #coding=utf-8 first_name='tiger' last_name='gao' full_name= f"{first_name} {l ...
- python编程系列---可迭代对象,迭代器和生成器详解
一.三者在代码上的特征 1.有__iter__方法的对象就是可迭代类(对象) 2.有__iter__方法,__next()方法的对象就是迭代器3.生成器 == 函数+yield 生成器属于迭代器, 迭 ...
- 教你Python字符串的基本操作:拆分和连接
摘要:由于字符串数据几乎无处不在,因此掌握有关字符串的交易工具非常重要.幸运的是,Python 使字符串操作变得非常简单,尤其是与其他语言甚至旧版本的 Python 相比时. 本文分享自华为云社区&l ...
- Python字符串(Str)详解
字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可 字符串的格式 b = "hello itcast. ...
- 【python基础】之str类字符串
str类字符串是不可变对象 1.创建字符串 s1 = str() #创建一个空字符串 s2 = str("hello") #创建字符串"hello" 2.处理字 ...
随机推荐
- Mac OS X终端下apache操作
重启apache:sudo /usr/sbin/apachectl restart 关闭apache:sudo /usr/sbin/apachectl stop 开启apache:sudo /usr/ ...
- groovy-正则表达式
Groovy使用~”pattern” 来支持正则表达式,它将使用给定的模式字符串创建一个编译好的Java Pattern 对象.Groovy也支持 =~(创建一个Matcher)和 ==~ (返回bo ...
- appium按钮定位,去掉弹出框
#coding=utf-8 这个一定要加上,不然脚本中注释中都不能有中文 ''' Created on 2015年7月2日 @author: liujuan ''' import sys reload ...
- HD2255奔小康赚大钱(最大权匹配模板)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- UVa OJ 175 - Keywords (关键字)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Many researchers are faced with an ever increasing numbe ...
- 51nod 1170 1770 数数字(动态规划)
解题思路:看到题后,直接想到分成两种情况: ①:a*b >9 这里又分成两种 1. n==1 a*b 直接是一个两位数 求得十位和个位(这里十位和个位不可能相等) 然后如果等于d 则结果=1 2 ...
- memcached安装和php-memcached扩展安装.update.2014-08-15
服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-1.3.0 .下载官网:http://www.danga.com另外,Memcache用到了libevent这个库用于 ...
- TFS2008 安装图解(详细版本)(转载)
由于公司准备上TFS,最近开始学习搭建TFS环境,并为同事讲解TFS的使用,在虚拟 机中搭建测试环境,遇到了很多问题,总结成一篇博客,跟大家交流一下: 我是从微软公司官方网站下载的TFS 2008 1 ...
- ASP.NET WebForm中用async/await实现异步出人意料的简单
1. 在.aspx中添加异步标记 <%@ Page Language="C#" Async="true"%> 2. 在.aspx.cs或者.ascx ...
- linux 的终端字体色和背景色的修改方法(二)
Linux终端下的颜色设置 2013-08-31 22:57:15 分类: LINUX 在 ANSI 兼容终端(例如 xterm.rxvt.konsole 等)里, 可以用彩色显示文本而不仅仅是 ...