首页 技术 正文
技术 2022年11月19日
0 收藏 958 点赞 2,436 浏览 2743 个字

1.原始字符串
python中的原始字符串以r开头,使用原始字符串可以避免字符串中转义字符带来的问题,例如写路径时 path = ‘c:\noway’,此时用 print path,其结果为:
c:
oway
如果用原始字符串 path = r’c:\noway’,则 print path,其结果为:c:\noway
2.python中没有字符的概念,字符即长度为1的字串。
3.字符串之间的转换:
1) 字符串和数字之间的转换:
int(x [,radix])
long(x [,radix])
float(x);
round(num [,digit])
complex(real [,imaginary]):转换为复数
ord(ch):转换为ascii码
2)数字和字符串之间的转换:
1 chr(x) unichr(x):将ascii码或者unicode转换为字符
2将数字转换为16或者8进制 oct(x) hex(x)
3 str(obj) 将任何对象转换为字符串
4.字符串中的处理方法

摘录其整理的表格,供参考:

类型
方法
注解
填充
center(width[, fillchar]),
ljust(width[, fillchar]),
rjust(width[, fillchar]),
zfill(width),
expandtabs([tabsize])

fillchar 参数指定了用以填充的字符,默认为空格
zfill的z为zero的缩写,顾名思义,是以字符0进行填充,用于数值输出
expandtabs()的tabsize 参数默认为8。它的功能是把字符串中的制表符(tab)转换为适当数量的空格。

删减
strip([chars]),
lstrip([chars]),
rstrip([chars])
chars为指定要去掉的字符,默认为空白字符,它由string.whitespace常量定义
变形
lower(),#全部小写
upper(),#全部小写
capitalize(),#首字母大写
swapcase(),#大小写交换
title()#每个单词第一个大写,其他小写
因为title() 函数并不去除字符串两端的空白符也不会把连续的空白符替换为一个空格,
所以建议使用string 模块中的capwords(s)函数,它能够去除两端的空白符,再将连续的空白符用一个空格代替。
>>> ‘ hello world!’.title()
‘ Hello World!’
>>> string.capwords(‘ hello world!’)
‘Hello World!’
切割
partition(sep),
rpartition(sep),
splitlines([keepends]),
split([sep [,maxsplit]]),
rsplit([sep[,maxsplit]])

partition()函数族是2.5版本新增的方法。它接受一个字符串参数,并返回一个3个元素的tuple 对象。
如果sep没出现在母串中,返回值是(sep, ‘’, ‘’);
否则,返回值的第一个元素是sep 左端的部分,第二个元素是sep 自身,第三个元素是sep 右端的部分。
参数maxsplit 是分切的次数,即最大的分切次数,所以返回值最多有maxsplit+1 个元素。
s.split() 和s.split(‘ ‘)的返回值不尽相同
>>> ‘ hello world!’.split()
[‘hello’, ‘world!’]
>>> ‘ hello world!’.split(‘ ‘)
[”, ”, ‘hello’, ”, ”, ‘world!’]

产生差异的原因在于当忽略sep 参数或sep参数为None 时与明确给sep 赋予字符串值时split() 采用两种不同的算法。
对于前者,split() 先去除字符串两端的空白符,然后以任意长度的空白符串作为界定符分切字符串
即连续的空白符串被当作单一的空白符看待;
对于后者则认为两个连续的sep 之间存在一个空字符串。因此对于空字符串(或空白符串),它们的返回值也是不同的:
>>> ”.split()
[]
>>> ”.split(‘2881064151 ‘)
[”]
连接
join(seq)
join() 函数的高效率(相对于循环相加而言),使它成为最值得关注的字符串方法之一。
它的功用是将可迭代的字符串序列连接成一条长字符串,如:
>>> conf = {‘host’:’127.0.0.1′,
… ‘db’:’spam’,
… ‘user’:’sa’,
… ‘passwd’:’eggs’}
>>> ‘;’.join(“%s=%s”%(k, v) for k, v in conf.iteritems())
‘passswd=eggs;db=spam;user=sa;host=127.0.0.1′
判定
isalnum(),
isalpha(),
isdigit(),
islower(),
isupper(),
isspace(),
istitle(),
startswith(prefix[, start[, end]]),
endswith(suffix[,start[, end]])
这些函数都比较简单,顾名知义。需要注意的是*with()函数族可以接受可选的start, end 参数,善加利用,可以优化性能。
另,自Py2.5 版本起,*with() 函数族的prefix 参数可以接受tuple 类型的实参,当实参中的某人元素能够匹配,即返回True。
查找
count( sub[, start[, end]]),
find( sub[, start[, end]]),
index( sub[, start[, end]]),
rfind( sub[, start[,end]]),
rindex( sub[, start[, end]])
find()函数族找不到时返回-1,index()函数族则抛出ValueError异常。
另,也可以用in 和not in 操作符来判断字符串中是否存在某个模板。
替换
replace(old, new[,count]),
translate(table[,deletechars])
replace()函数的count 参数用以指定最大替换次数
translate() 的参数table 可以由string.maketrans(frm, to) 生成
translate() 对unicode 对象的支持并不完备,建议不要使用。
编码
encode([encoding[,errors]]),
decode([encoding[,errors]])
这是一对互逆操作的方法,用以编码和解码字符串。因为str是平台相关的,它使用的内码依赖于操作系统环境,
而unicode是平台无关的,是Python内部的字符串存储方式。
unicode可以通过编码(encode)成为特定编码的str,而str也可以通过解码(decode)成为unicode。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860