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 拼接成字符串? 如何替换字符串? 如何去除字符串中的空格? 如何子字符串是否包含 ...
随机推荐
- ASP.NET Core的Data Protect(数据保护)的学习和应用
转载请注入出处: https://home.cnblogs.com/u/zhiyong-ITNote/ dotnet core中提供了一个新的身份验证框架Identity,它不同于dot net下的身 ...
- PAT (Advanced Level) Practise 1004 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...
- Web API之service worker
一.参考链接 https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API http://www.alloyteam.com/ ...
- cf 443
题目链接 A,对于每一位可以暴力输入,看输出是什么,然后就有2x2中对应方式,然后可以用3次运算搞了,好像网上在悬赏最多只用2次搞出来的. B,这个题可以先处理每个串内部的情况,再处理连接处的情况,代 ...
- STM32——C语言知识点:指针、结构体
/* ============================================================================ Name : Cyuyanfuxi.c ...
- [NOIp2013提高组]积木大赛/[NOIp2018提高组]铺设道路
[NOIp2013提高组]积木大赛/[NOIp2018提高组]铺设道路 题目大意: 对于长度为\(n(n\le10^5)\)的非负数列\(A\),每次可以选取一个区间\(-1\).问将数列清零至少需要 ...
- linux VPS服务器的一些配置
SSH密钥登录让Linux VPS/服务器更安全 2011年01月10日 上午 | 作者:VPSer 随着PHP越来越流行,Linux VPS/服务器的使用也越来越多,Linux的安全问题也需要日渐加 ...
- Incorrect username or password ( access token )解决
Q:Git提交时,给出提示Incorrect username or password ( access token ) K: 此处是用户名或者密码有误,建议解决方法两种.具体看哪一种可行,可试. 第 ...
- Python基础-修改excel、redis、接口开发、组织代码
pymysql模块补充内容 1. 游标.description():显示表的字段属性 (什么是游标:游标用于交互式应用,就好比word里的光标一样,要修改某个地方,要先把光标移动到这里) 用好这个方法 ...
- 【网站seo优化】SEO优化每天的工作内容是什么?
[网站seo优化]SEO优化每天的工作内容是什么?从未知的领域来到seo,感到搜索引擎无比神奇,接触seo久了,有每天必做的工作内容,大量的seo从业者,每天的工作内容大同小异,主要做的工作有通过相应 ...