【python】字符串函数
1.String模块中的常量:
string.digits:数字0~9
string.letters:所有字母(大小写)
string.lowercase:所有小写字母
string.printable:可打印字符的字符串
string.punctuation:所有标点
string.uppercase:所有大写字母
import string
>>> string.uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.digits
'0123456789'
>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
2.find函数在一个较长的字符串中查询子字符串,返回子串所在位置最左端索引,没有找到返回-1
>>> title = "Monty Python's Flying Circus"
>>> title.find('Monty')
0
>>> title.find('monty')
-1
可以选择起始点和结束点
>>> title.find('Python')
6
>>> title.find('Python', 3)
6
>>> title.find('Python', 3, 10)
-1
3.join函数在队列中添加元素(只能操作于字符串,返回一个修改后的字符串,但是原字符串不改变),它的逆函数为split,split函数将字符串分割成序列,返回该序列,原字符串不改变
>>> seq = ['1', '2', '3', '4', '5']
>>> sep = '+'
>>> sep.join(seq)
'1+2+3+4+5'
>>> seq
['1', '2', '3', '4', '5'] >>> dirs = '', 'usr', 'bin', 'env'
>>> '/'.join(dirs)
'/usr/bin/env'
>>> print 'C:' + '\\'.join(dirs)
C:\usr\bin\env >>> word = '1+2+3+4+5'
>>> word.split('+')
['1', '2', '3', '4', '5']
>>> word
'1+2+3+4+5'
4.lower函数返回字符串的小写字母版
>>> 'fafDAWdfaweDWED'.lower()
'fafdawdfawedwed' 扩展:title函数:首字母大写,其他小写
>>> "that's all folks".title()
"That'S All Folks"
5.replace函数返回某字符串所有匹配项均被替换之后得到的字符串,原字符串不改变
>>> word = 'this is a test'
>>> word.replace('is', 'eez')
'theez eez a test'
>>> word
'this is a test
6.strip函数去除两侧(不包括内部)空格的字符串,原序列不变
>>> word = ' this is test '
>>> word.strip()
'this is test'
>>> word
' this is test ' 可在strip()加入参数,以去除想要去掉的指定字符
>>> '*** SPAM * for * everyone!!! ***'.strip('*')
' SPAM * for * everyone!!! '
>>> '*** SPAM * for * everyone!!! ***'.strip('* ')
'SPAM * for * everyone!!!'
【python】字符串函数的更多相关文章
- 【C++实现python字符串函数库】strip、lstrip、rstrip方法
[C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- 【C++实现python字符串函数库】一:分割函数:split、rsplit
[C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...
- 【276】◀▶ Python 字符串函数说明
参考:Python 字符串函数 01 capitalize 把字符串的第一个字符大写,其他字母变小写. 02 center 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串. ...
- (转)python字符串函数
原文:https://www.cnblogs.com/emanlee/p/3616755.html https://blog.csdn.net/luoyhang003/article/details/ ...
- Python 简明教程 --- 8,Python 字符串函数
微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 好代码本身就是最好的文档.当你需要添加一个注释时,你应该考虑如何修改代码才能不需要注释. -- St ...
- python字符串函数总结
字符串函数主要分为: 查找: strs = "this is a line of text for test" index1 = strs.find("is") ...
- 在 R 中使用 Python 字符串函数
sprintf( )函数很强大,但并非适用于所有应用场景.例如,如果一些部分在模板中多次出现,那么就需要多次写一样的参数.这通常会使得代码冗长而且难以修改:sprintf("%s, %d y ...
- python 字符串函数
split函数:将字符串分割成序列 str.split("分隔符") 一般可以这样用 list = [n for n in str.split],这样可以得到一个新的序列 str ...
- python 字符串函数功能快查
0.dir(str)一.有字符发生转换1.capitalize,字符串的第一个字符大写2.casefold,将所有字符小写,Unicode所有字符均适用3.lower,将所有字符小写,只适用ASCii ...
随机推荐
- 利用CNN进行流量识别 本质上就是将流量视作一个图像
from:https://netsec2018.files.wordpress.com/2017/12/e6b7b1e5baa6e5ada6e4b9a0e59ca8e7bd91e7bb9ce5ae89 ...
- 【css】弹性盒模型
弹性盒模型flexBox 弹性盒模型是c3的一种新的布局模式 它是指一种当页面需要适应不同屏幕大小以及设备类型时,确保元素有恰当行为的布局方式. 引入弹性盒模型布局的目的是提供一种更有效的方法来对一个 ...
- code format using astyle
script context : #! /bin/bash for f in $(find . -name '*.c' -or -name '*.cpp' -or -name '*.h' -type ...
- sgu 125 Shtirlits dfs 难度:0
125. Shtirlits time limit per test: 0.25 sec. memory limit per test: 4096 KB There is a checkered fi ...
- 一个Flex 对话框的坑
最近在项目中遇到一个问题,在Flex中使用Alert.show("this is content!", "title");发现对话框可以弹出来,但是文本始终不显 ...
- <constant name="struts.devMode" value="false" />
在开发中,我们常常会遇到<constant name="struts.devMode" value="false" />,这是struts2的特性, ...
- IOS常用代码整理
常用代码整理: 12.判断邮箱格式是否正确的代码: //利用正则表达式验证 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRege ...
- 2018C语言助教总结
回顾 很荣幸得到各位老师的认可,担任计科3班和4班的C语言课程助教,很感谢车老师和牛老师一学期的帮助,使得我更好的担任助教一职.我班学生59名,很愉快的与同学们度过一个美好的学期,其实作为助教同样从学 ...
- 使用Inno Setup Compiler制作安装软件包
前言 项目开发完成之后,需要程序打包发行,本文使用Inno Setup工具制作安装软件包. 系统环境 系统:win7_x64 工具:Inno Setup Complier 实现步骤 1.下载安装Inn ...
- ZZNU 2125:A + B 普拉斯(傻逼题+大数加法)
2125: A + B 普拉斯 时间限制: 1 Sec 内存限制: 128 MB 提交: 94 解决: 28 [提交] [状态] [讨论版] [命题人:admin] 题目描述 "别人总说 ...