在本博客 《Python字符串系列》 中,将介绍以下内容:

  1. Python内置的str对象及操作
  2. 字符串的格式化
  3. Python中的正则表达式
  4. re模块

  本文将介绍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])

  

19.
zfill()
s.zfill(width)

  

(原创)Python字符串系列(1)——str对象的更多相关文章

  1. Python语言系列-08-面向对象3

    反射 #!/usr/bin/env python3 # author: Alnk(李成果) # 反射 # hasattr getattr setattr delattr class Animal(ob ...

  2. Python坑系列:可变对象与不可变对象

    在之前的文章 http://www.cnblogs.com/bitpeng/p/4748148.html 中,大家看到了ret.append(path) 和ret.append(path[:])的巨大 ...

  3. Python语言系列-07-面向对象2

    重构父类__init__方法 #!/usr/bin/env python3 # author:Alnk(李成果) # 需求:Dog类要新增一个实例属性,但是Cat类不需要 class Animal(o ...

  4. Python语言系列-06-面向对象1

    楔子 #!/usr/bin/env python3 # author:Alnk(李成果) # 人狗大战例子引入面向对象 # 版本1 def hero(name, sex, hp, ce, level= ...

  5. python字符串系列--2

    #!/usr/bin/python #coding=utf-8 first_name='tiger' last_name='gao' full_name= f"{first_name} {l ...

  6. python编程系列---可迭代对象,迭代器和生成器详解

    一.三者在代码上的特征 1.有__iter__方法的对象就是可迭代类(对象) 2.有__iter__方法,__next()方法的对象就是迭代器3.生成器 == 函数+yield 生成器属于迭代器, 迭 ...

  7. 教你Python字符串的基本操作:拆分和连接

    摘要:由于字符串数据几乎无处不在,因此掌握有关字符串的交易工具非常重要.幸运的是,Python 使字符串操作变得非常简单,尤其是与其他语言甚至旧版本的 Python 相比时. 本文分享自华为云社区&l ...

  8. Python字符串(Str)详解

    字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可 字符串的格式 b = "hello itcast. ...

  9. 【python基础】之str类字符串

    str类字符串是不可变对象 1.创建字符串 s1 = str() #创建一个空字符串 s2 = str("hello") #创建字符串"hello" 2.处理字 ...

随机推荐

  1. Tomcat tomcat-users.xml详解

    conf/tomcat-users.xml <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rol ...

  2. 旅行(Dijkstra)问题

    问题:输入: 输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个:   接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车 ...

  3. Java的多线程机制系列:(三)synchronized的同步原理

    synchronized关键字是JDK5之实现锁(包括互斥性和可见性)的唯一途径(volatile关键字能保证可见性,但不能保证互斥性,详细参见后文关于vloatile的详述章节),其在字节码上编译为 ...

  4. Codeforces Round #342 (Div 2) 解题报告

    除夕夜之有生之年CF第一场 下午从奶奶家回到姥姥家,一看还有些时间,先吃点水果陪姥姥姥爷聊了会儿,再一看表,5:20....woc已经开场20分钟了...于是抓紧时间乱搞.. **A. Guest F ...

  5. 手动搭建SpringMVC报错

    猜测这个是由于自己在搭建时缺少包造成的,后来将按照自己之前的项目将包补齐,tomcat就不报错了,看来还是要学习maven 这样就不会缺少包了

  6. 【poj2546】 Circular Area

    http://poj.org/problem?id=2546 (题目链接) 题意 求两圆的面积交 Solution 一道水题Wa死我了,肯定是昨晚搞太晚的缘故= =. 两圆的位置关系有5种,而这里要求 ...

  7. [NOIP2011] 普及组

    数字反转 小模拟 #include<cstdio> #include<iostream> #include<cstring> using namespace std ...

  8. JSP将表单提交并在本页中显示

    代码如下: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8& ...

  9. UVa 12505 Searching in sqrt(n)

    传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...

  10. eclipse中使用git

    有的eclipse已经自带了Git了,就不用安装了.如果,想重新安装,可以先卸载GIT,卸载 不同eclipse卸载不一样: 1.在Eclipse中依次点击菜单"Help"-> ...