(原创)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.处理字 ...
随机推荐
- 【HDU 1003】 Max Sum
题 题意 需要在o(n)时间内,求最大连续的子序列的和,及其起点和终点. 分析 一种方法是一边读,一边维护最小的前缀和 s[i] ,然后不断更新 ans = max(ans,s[j] - s[i]), ...
- 【Matplotlib】图例分开显示
作图时图例往往都会出现一个图例框内,如果需要不同类型的图例分别显示,比如显示两个图例. 基本上,出现两个图例的话,需要调用两次 legend .第一次调用,你需要将图例保存到一个变量中,然后保存下来. ...
- bzoj 3437 斜率优化DP
写题解之前首先要感谢妹子. 比较容易的斜率DP,设sum[i]=Σb[j],sum_[i]=Σb[j]*j,w[i]为第i个建立,前i个的代价. 那么就可以转移了. /**************** ...
- BZOJ2120 数颜色
Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成 一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同 ...
- TCP Socket Establish;UDP Send Package Process In Kernel Sourcecode Learning
目录 . 引言 . TCP握手流程 . TCP connect() API原理 . TCP listen() API原理 . UDP交互过程 . UDP send() API原理 . UDP bind ...
- UVa 12505 Searching in sqrt(n)
传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...
- appium向右滑动
/*** * 右滑1/2屏幕 / public static void slideRight(){ int x=driver.manage().window().getSize().width; in ...
- Laravel5.1 启动详解
借鉴: Laravel所有请求的入口文件是:/public/index.php,代码如下 <?php /*|------------------------------------------- ...
- Mac上的终端(Terminal)启动缓慢
最近重装10.9系统,装完后,发现终端(Terminal)启动之前1秒都不用,现在却需要5-10秒,搜寻了下,发现是终端的统日志导致的问题,只需要执行下下面的命令,终端就又身轻如燕了! sudo rm ...
- UVa OJ 175 - Keywords (关键字)
Time limit: 3.000 seconds限时3.000秒 Problem问题 Many researchers are faced with an ever increasing numbe ...