python字符串规则_python字符串(⼀)变量命名规则
在讲解python字符串之前,先穿插⼀下变量的命名规则:
1、变量名只能包含字母、数字和下划线,不能以数字开头
2、变量名不能包含空格,可以使⽤下划线分隔其中的单词
3、不要将python关键字和函数名⽤作变量名
4、变量应该既简单⼜具有描述性
5、谨慎使⽤⼩写字母i和⼤写字母O,容易看成0和1
字符串
——使⽤单引号' ' 或 双引号" " 或三引号""" """括起来的⼀串字符
python字符串有很多⽅法,⼤致分为以下⼏类:
字符串⽅法:
⼀、变形lower upper capitalize title swapca
string.lower()
——将字符串全部变成⼩写
string.upper()
——将字符串全部变成⼤写
string.capitalize()
——将字符串⾸字母⼤写
string.title()
——将每⼀个单词的⾸字母⼤写,并将单词中⾮⾸字母转换成⼩写
string.swapca()
天然气英语——将字符串⼤写字母变为⼩写字母,⼩写字母变为⼤写字母
landmark下⾯依依举例说明:
>>> str1="hello world!"
>>>str1.lower()'hello world!'
>>>str1.upper()'HELLO WORLD!'
>>>str1.capitalize()'Hello world!'
>>>str1.title()'Hello World!'
>>>str1.swapca()'HELLO WORLD!'
⼆、删减strip lstrip rstrip
西安电脑培训string.strip([chars])
——去除字符串两端的空⽩符
string.lstrip([chars])
evaluate是什么意思
—
—去除字符串左边的空⽩符
string.rstrip([chars])
——去除字符串右边的空⽩符
下⾯依依列举说明:
>>> str2="hello world!"
>>> print(str2)
hello world!>>>str2.strip()'hello world!'
>>>str2.lstrip()'hello world!'
you are not alone歌词翻译
>>>str2.rstrip()'hello world!'
三、分切partition rpartition splitlines split rsplit
string.partition(p)
—
—此⽅法返回⼀个三元的tuple,分别是 p左边的字符串,分隔符p本⾝和分隔符p右边的字符串
>>> str1="/kudangren/"
>>> str1.partition("://")
('http', '://', '/kudangren/')
string.rpartition(p)
——与partition⼀样,不同的是从右到左开始匹配
string.splitlines(keepends)minded什么意思
——Python splitlines() 按照⾏('\r', '\r\n', \n')分隔,返回⼀个包含各⾏作为元素的列表,如果参数 keepends 为 Fal,不包含换⾏符,如果为 True,则保留换⾏符。
keepends -- 在输出结果⾥是否去掉换⾏符('\r', '\r\n', \n'),默认为 Fal,不包含换⾏符,如果为 True,则保留换⾏符。
>>> str1 = 'ab c\n\nde fg\rkl\r\n'
>>>str1.splitlines()
['ab c', '', 'de fg', 'kl']>>> str2 = 'ab c\n\nde fg\rkl\r\n'
>>>str2.splitlines(True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']
string.split([p[,maxsplit]])
——通过指定分隔符对字符串进⾏切⽚,返回分割后的字符串列表。如果参数maxsplit有指定值,则仅分隔 maxsplit 个⼦字符串
>>>a=""
>>>a.split('.')
['yl','text','txt']>>>a.split('.',1)
['alvy','']
string.rsplit([p[,maxsplit]])
——同split,不同的是从右开始分割,还以字符串a举例,应⽤此⽅法我们可以判定⽂件类型
>>>a.rsplit('.',1)
['st','txt']
四、连接join
string.join(q)
——join() ⽅法⽤于将序列中的元素以指定的字符连接⽣成⼀个新的字符串。
q:要连接的元素序列
例1
>>> str1="-"
>>> q=["2017","12","11"]>>>str1.join(q)'2017-12-11
例2
斯图尔特和帕丁森
conf = {'host':'127.0.0.1',
...'db':'spam',
freetranslation...'ur':'sa',
...'passwd':'eggs'}>>> ';'.join("%s=%s"%(k, v) for k, v inconf.iteritems())'passswd=eggs;db=spam;ur=sa;host=127.0.0.1'五、判定isalnum isalpha isdigit islower isupper isspace istitle startswith endswith
string.isalnum()
-检测字符串是否由字母和数字组成,如果string⾄少有⼀个字符并且所有字符都是字母或数字则返回 True,否则返回 Fal。>>> str = "this2009" #字符中没有空格
>>>str.isalnum()
True>>> str = "this is wow">>>str.isalnum()
Fal
string.isalpha()
-检测字符串是否只由字母组成,如果字符串⾄少有⼀个字符并且所有字符都是字母则返回 True,否则返回 Fal。
>>> str = "this" #No space & digit in this string
>>>str.isalpha()
True>>> str = "this is wow"
>>>str.isalpha()
Fal
string.isdigit()
-检测字符串是否只由数字组成,如果字符串只包含数字则返回 True 否则返回 Fal。
>>> str = "19920323" #No space & digit in this integer
>>>str.isalpha()
True>>> str = "this is wow"
>>>str.isalpha()
Fal
string.islower()
-检测字符串是否由⼩写字母组成,如果字符串中包含⾄少⼀个区分⼤⼩写的字符,并且所有这些(区分⼤⼩写的)字符都是⼩写,则返回True,否则返回 Fal。
>>> str = "bu dao weng xiansheng"
>>>str.isalpha()
True>>> str = "This is wow"
>>>str.isalpha()
Fal
string.isupper()
-与islower相反,可以islower做为参照。
string.isnumeric()
-检测字符串是否只由数字组成。这种⽅法是只针对unicode对象。
注:定义⼀个字符串为Unicode,只需要在字符串前添加 'u' 前缀即可,具体可以查看本章节例⼦。
>>> str = u"this2009"
金太阳英语
>>>str.isnumeric()
Fal>>> str = u"23443434"
>>>str.isnumeric()
True
string.isspace()
-检测字符串是否只由空格组成,如果字符串中只包含空格,则返回 True,否则返回 Fal。
>>> str=" "iota
>>>str.isspace()
True>>> str="hellow world!"
>>>str.isspace()
Fal
string.istitle()
-检测字符串中所有的单词拼写⾸字母是否为⼤写,且其他字母为⼩写,如果字符串中所有的单词拼写⾸字母是否为⼤写,且其他字母为⼩写则返回 True,否则返回 Fal
>>> str = "This Is Wow"
>>> printstr.istitle()
True>>>str = "This is wow"
>>> printstr.istitle()
Fal
string.startswith(prefix[,start[,end]])
-⽤于检查字符串是否是以指定⼦字符串开头,如果是则返回 True,否则返回 Fal。如果参数 start 和 end 指定值,则在指定范围内检查。
prefix--检测的字符串
start--可选参数⽤于设置字符串检测的起始位置
end--可选参数⽤于设置字符串检测的结束位置。
>>> str = "this is wow";>>> str.startswith( 'this')
True>>> str.startswith( 'is', 2, 4)
True>>> str.startswith( 'this', 2, 4)
Fal
-⽤于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回Fal。可选参数"start"与"end"为检索字符串的开始与结束位置。
suffix--该参数可以是⼀个字符串或者是⼀个元素
start--字符串中的开始位置
end--字符中结束位置
>>> str = "this is wow"
>>> suffix = "wow"
>>&dswith(suffix)
True>>> dswith(suffix,20)
True>>> suffix = "is"
>>> dswith(suffix, 2, 4)
True>>> dswith(suffix, 2, 6)
Fal
六、查找count find index rfind rindex
-⽤于统计字符串⾥某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
sub--搜索的⼦字符串
start--字符串开始搜索的位置。默认为第⼀个字符,第⼀个字符索引值为0
end--字符串中结束搜索的位置。字符中第⼀个字符的索引为 0。默认为字符串的最后⼀个位置
>>> str = "this is wow"
>>> sub="i"
>>> unt(sub,4,40)2
>>> sub="wow"
>>&unt(sub)1