Python之string
1、string模块支持哪些字符形式?分别是什么。
string支持的字符形式有:
('_re', '====>', <module 're' from 'C:\Python25\lib\re.pyc'>)
('ascii_letters', '====>', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
('ascii_lowercase', '====>', 'abcdefghijklmnopqrstuvwxyz')
('ascii_uppercase', '====>', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
('digits', '====>', '0123456789')
('hexdigits', '====>', '0123456789abcdefABCDEF') #不太理解
('letters', '====>', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')#
('lowercase', '====>', 'abcdefghijklmnopqrstuvwxyz')
('octdigits', '====>', '01234567') #不太理解
('printable', '====>', '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c')#全部内容
('punctuation', '====>', '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~') #标点符号
('uppercase', '====>', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
('whitespace', '====>', '\t\n\x0b\x0c\r ') #所有空字符
2、有哪些字符操作符?
简单做个分类:
关于大小写的函数:①操作首字母capitalize,capwords。②全部字母:upper,lower,swapcase
字符查找:①查找次数:count,②查找位置 find,index
字符串内容修改:①拆分 split ,②连接join,本人习惯用“+”,③对齐 rjust,ljust,center,④替换replace,替换空白字符 trim,ltrim,rtrim。
补充一些:拆分 str.partition(seq),根据seq拆分字符串,左起遇到第一个seq后,将str拆分,返回三个元组,如 hello word,seq=‘l’,返回结果: ('he', 'l', 'lo word')
string.capitalize(s)返回字符串s的一个副本,这个副本的第一个字符大写。
>>> s="hello world"
>>> string.capitalize(s)
'Hello world'
string.capwords(s)每个单词的首字母大写。
>>> string.capwords(s)
'Hello World'
>>> string.center(s,20)
' hello world '
>>> string.center(s,2)
'hello world'
>>> string.center(s,20,'*')
'****hello world*****'
string.center(s,width[,fillchar])函数,用指定的宽度来返回一个居中版的s,如果需要的话,就用fillchar进行填充,默认是空格。但是不会对s进行截取。即如果s的长度比width大,也不会对s进行截取。
>>> string.count(s,"h")
1
string.count(s,sub[,start[,end]])返回在s[start:end]范围内子串sub在字符串s当中出现的次数
>>> string.find(s,"a")
-1
string.find(s,sub[,start[,end]])返回在s[start:end]范围内子串sub在字符串s当中出现的最小下标,没有找到返回-1
string.index(s,sub[,start[,end]])与string.find方法类似,只不过当没有找到子串sub的时候,会抛出ValueError异常
>>> string.index(s,"a")
Traceback (most recent call last):
File "<pyshell#233>", line 1, in <module>
string.index(s,"a")
File "C:\Python25\lib\string.py", line 326, in index
return s.index(*args)
ValueError: substring not found
>>> string.ljust(s,20)
'hello world '
string.ljust(s, width[, fillchar])字符串的左对齐,
那么string.rjust()就是右对齐。
>>> string.upper(s)
'HELLO WORLD'
>>> string.lower(s)
'hello world'
string.upper()和string.lower()比较简单。就是全部转换为大写或者小写
>>> string.swapcase(s)
'HELLO WORLD'
string.swapcase()实现大小写的转换。将大写转换为小写,将小写转换为大写。
>>> s=" hello world "
>>> string.strip(s)
'hello world'
string.strip(s)剔除字符串s左右空格
>>> string.lstrip(s)
'hello world '
>>> string.rstrip(s)
' hello world'
string.lstrip(s)和string.rstrip(s)分别剔除字符串左、右边的空格
>>> string.zfill(s,20)
'000000 hello world '
>>> string.zfill(s,2)
' hello world '
string.zfill(s,width)与center类似,不过这里的填充使用"0"来替代。
s="abc"
>>> x=string.maketrans(string.ascii_letters,string.ascii_letters[2:]+string.ascii_letters[:2])
>>> string.translate(s,x)
'cde'
string.maketrans()和string.translate()一般配合使用,用maketrans定义字符串的转换规则,然后用translate来实现。
我们可以用它来实现swapcase()方法
>>> x=string.maketrans(string.ascii_letters,string.letters)
>>> string.translate("AbCdEf",x)
'aBcDeF'
>>> string.translate("Ab CdE f",x)
'aB cDe F'
>>> string.split("hello world")
['hello', 'world']
string.split(s, sep=None, maxsplit=-1)用sep拆分s,返回拆分后的列表,如果sep没有提供或者为None,那么默认的就是空格
string.join的功能刚好与其相反。
>>> l=string.split("hello world")
>>> string.join(l)
'hello world'
join(list [,sep])是用sep把list组合成一个字符串返回。
Python之string的更多相关文章
- python的string用法
s.strip().lstrip().rstrip(',') S.lower() #小写 S.upper() #大写 S.swapcase() #大小写互换 S.capitalize() #首字母大写 ...
- Python 常用string函数
Python 常用string函数 字符串中字符大小写的变换 1. str.lower() //小写>>> 'SkatE'.lower()'skate' 2. str.upper ...
- python中string模块各属性以及函数的用法
任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作. python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串 ...
- python中string格式化
python中可以对string, int, float等数据类型进行格式化操作.下面举例来说明一些常用操作. 先贴出 python 对 String Formatting Operations 讲解 ...
- PyQt的QString和python的string的区别
转载于http://blog.chinaunix.net/uid-200142-id-4018863.html python的string和PyQt的QString的区别 python string和 ...
- python中string.casefold和string.lower区别
string.casefold和string.lower 区别 python 3.3 引入了string.casefold 方法,其效果和 string.lower 非常类似,都可以把字符串变成小写, ...
- 浅析python的string.Template
摘自:python参考手册. string模块定义了一种新字符串类型Template,简化了特定的字符串置换操作, Template定义一个类 1.template(s), #s是字符串 s='he ...
- 牛人总结python中string模块各属性以及函数的用法,果断转了,好东西
http://blog.chinaunix.net/uid-25992400-id-3283846.html http://blog.csdn.net/xiaoxiaoniaoer1/article/ ...
- python字符串(string)方法整理
python中字符串对象提供了很多方法来操作字符串,功能相当丰富. print(dir(str)) [..........'capitalize', 'casefold', 'center', 'co ...
- python 历险记(一)— python 的String,集合(List,元组,Dict)
目录 引言 String 有哪些有用的方法? 如何拼接字符串? 如何分隔字符串? 如何获取字符串长度 如何将 list 拼接成字符串? 如何替换字符串? 如何去除字符串中的空格? 如何子字符串是否包含 ...
随机推荐
- jarvis OJ WEB题目writeup
0x00前言 发现一个很好的ctf平台,题目感觉很有趣,学习了一波并记录一下 https://www.jarvisoj.com 0x01 Port51 题目要求是用51端口去访问该网页,注意下,要用具 ...
- UVA 2519 Radar Installtion
思路: #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> ...
- spring 启动异常Failed to read candidate component class
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: ...
- SpringIOC和DI
1.Spring的概念 Spring的绿草丛(一) Spring轻量级框架, Java EE的春天,当前主流框架目标使现有技术更加易用,推进编码最佳实践内容IoC容器AOP实现数据访问支持简化JDBC ...
- [SPOJ-CIRU]The area of the union of circles/[BZOJ2178]圆的面积并
[SPOJ-CIRU]The area of the union of circles/[BZOJ2178]圆的面积并 题目大意: 求\(n(n\le1000)\)个圆的面积并. 思路: 对于一个\( ...
- BZOJ2042 : [2009国家集训队]Will的烦恼
不难发现题中过程对应着动态维护关于$C$的最大生成树. 为了让$D$最大,同时让字典序最大,那么最后得到的一定是按$pair(C,D,编号)$排序的最大生成树. 对于每条非树边$(u,v,C)$,那么 ...
- 软件开发 [CJOJ 1101] [NOIP 模拟]
Description 一个软件开发公司同时要开发两个软件,并且要同时交付给用户,现在公司为了尽快完成这一任务,将每个软件划分成m个模块,由公司里的技术人员分工完成,每个技术人员完成同一软件的不同模块 ...
- LOJ #10084. 「一本通 3.3 练习 1」最小圈(二分+SPFA判负环)
题意描述: 见原LOJ:https://loj.ac/problem/10084 题解: 假设所求的平均最小值为X,环上各个边的权值分别为A1,A2...Ak,可以得到: X=(A1+A2+A3+.. ...
- IIS中找不到dll文件的依赖项问题
1. 文件是否被锁定了2. 文件是否具有了everyone用户的读写权限.3. 文件是不是编译为了AnyCPU模式.4. 文件依赖的文件是否在bin目录下存在5. 停止IIS,把.net Framew ...
- C#自动识别文件编码
以下代码源自:http://www.cnblogs.com/stulzq/p/6116627.html /// <summary> /// 用于取得一个文本文件的编码方式(Encoding ...