Python基础教程——第三章 使用字符串
来源:互联网 发布:linux网络故障排查 编辑:程序博客网 时间:2024/06/05 17:49
本章主要介绍字符串的格式化和常用方法。字符串是内建序列,因此序列的相关操作字符串都可以使用(索引,分片,乘法,in,求长度,最大最小值)。但是有一点需要注意,字符串是不可以改变的!
字符串格式化
简单介绍
1.字符串格式化使用字符串格式化操作符‘%’,‘%’左侧放置一个字符串,右侧放置希望被格式化的值。可以使用一个值,也可以使用多个值的元组或字典。一般使用元组:
>>> format="Hello,%s.%s enough for you?">>> values=('world','Hot')>>> print format % values>Hello,world.Hot enough for you?
注意:
如果使用列表或其他序列代替元组,那么序列会被解释为一个值,只有元组和字典可以格式化一个以上的值.
在格式化字符串中包含百分号,必须使用%%
2. 如果要格式化实数(浮点数),可以使用f说明转换说明符的类型,同时提供所需要的精度。
>>> format="Pi with threee decimals:%.3f">>> from math import pi>>> print(format%pi)>Pi with three decimals:3.142
3.模板字符串
string模块提供了另外一种格式化值得方法:模板字符串。substitute这个模板方法使用传递进来的关键字参数foo替换字符串中的$foo。
>>> from string import Template>>> s = Template('$x,glorious $x!')>>> s.substitute(x='slurm')>'slurm,glorious slurm!'
如果替换的是单词的一部分,则需要用括号,来指明结尾:
>>> s=Template("It's ${x}tastic!")>>> s.substitute(x='slurm')>"It's slurmtastic!"
#可以使用$$插入美元符号>>> s=Template>>> s=Template("Make $$ selling $x!")>>> s.substitute(x='slurm')>'Make $ selling slurm!'
'Make $ selling slurm!'>>> #使用字典变量提供值/名称对>>> s=Template('A $thing must never $action.')>>> d={}>>> d['thing']='gentleman'>>> d['action']='show his socks'>>> s.substitute(d)'A gentleman must never show his socks.'
字符串格式化:完整版
字符串方法
1.find
find方法可以在一个较长的字符串中查找子串,返回子串所处位置最左端的index,没有找到则返回-1。
>>> title="Monty Python's Flying Circus">>> title.find('Python')>6
find也可以接受可选的起点和终点:
>>> subject="$$$ Get rich now!!!$$$">>> subject.find('$$$',1)>19
2.join
join是split方法的逆方法,用于连接序列中的元素。注意join连接的序列必须是字符串。
>>> seq=['1','2','3','4','5']>>> ''.join(seq)>'12345'
>>> dirs='','usr','bin','env'>>> '/'.join(dirs)>'/usr/bin/env'>>> print('C:'+'\\'.join(dirs))>C:\usr\bin\env
3.split
split 用来把字符串分割成序列:
>>> '1+2+3+4+5'.split('+')>['1', '2', '3', '4', '5']#默认使用空格作为分隔符>>> 'Using the defalut'.split()>['Using', 'the', 'defalut']
4.lower
lower将字符串转换为小写字母版。
>>> 'Hello,WOrld!'.lower()>'hello,world!'
与lower相关的还有title方法,将字符串转换为标题——所有单词首字母大写,其余字母小写。
>>> "that's all folks".title()>"That'S All Folks"
还有一个string模块内的capwords函数:
>>> import string>>> string.capwords("that's all, folks")>"That's All, Folks"
5.replace
返回某字符串所有匹配项均替换后的结果。
>>> 'This is a test'.replace('is','eez')>'Theez eez a test'
6.strip
strip方法返回去除两侧(不包括内部)空格的字符串。
>>> ' internal whitesppace is kept '.strip()>'internal whitesppace is kept'
也可以指定需要去除的字符。
>>> '***SPAM * for * everyone!!!***'.strip(' *!')>'SPAM * for * everyone'
7.translate
translate 用于替换字符串的某些部分,但是与replace不同的是,translate只处理单个字符。在translate之前需要完成转换表,转换表是某字符替换某字符的对应关系。使用string模块中的maketrans函数产生转换表。
maketrans接受两个等长字符串,表示第一个字符串中的每个字符都由第二个字符串对应位置的字符替换。
>>> a='Hello,worId!'>>> t=a.maketrans('I','a')>>> a.translate(t)>'Hello,worad!'
- Python基础教程——第三章 使用字符串
- Python基础教程第三章——使用字符串
- Python 基础教程第三章 使用字符串
- 《python基础教程》第三章 使用字符串
- Python基础教程-第三章-使用字符串
- python基础教程读书笔记——第三章 字符串
- Python基础教程 | 第三章 字符串
- 第三章-使用字符串——python基础教程(第二版)笔记
- Python基础教程——3使用字符串
- Python基础教程笔记——使用字符串
- Python基础教程笔记——使用字符串
- Python基础教程笔记——使用字符串
- Python基础教程学习笔记----第三章 字符串
- python基础教程学习笔记 第三章 字符串
- python学习 第三章 使用字符串 《python基础教程第二版修订版》
- Python学习笔记【三】——《python基础教程》:: 使用字符串
- Python 第三章 使用字符串
- Python基础教程(第三章)
- Linux下安装及操作SQL数据库
- 使用mockserver来进行http接口mock
- 缓冲区与Buffer
- APUE中文第三版11章读写锁的一个可运行的例子
- PHP入门1.5
- Python基础教程——第三章 使用字符串
- 重学Spring——IOC
- 50. Pow(x, n)
- 【u107】数字游戏(bds)
- [leetcode] 80. Remove Duplicates from Sorted Array II
- 二叉树的三种遍历图解
- sklearn 源码解析 基本线性模型 base.py
- [又值奥运季] 2016年里约奥运会--8月22日赛事
- 信息系统项目管理师考试战区图