Python3基础知识之字符串

1、运算符 *

>>> b=a*5
>>> b
'pythonpythonpythonpythonpython'
>>> b.replace('t','12')
'py12honpy12honpy12honpy12honpy12hon'
>>> b.replace('t','12',1)
'py12honpythonpythonpythonpython'
>>> b.replace('t','12',-1)
'py12honpy12honpy12honpy12honpy12hon'
>>> 
第三个参数只能代表个数,不能用其他值,比如负数

>>> b.replace('np','n p')
'python python python python python'
>>> b = b.replace('np','n p')
>>> b
'python python python python python'
>>> b.split()
['python', 'python', 'python', 'python', 'python']
>>> b
'python python python python python'
>>> b.split('h')
['pyt', 'on pyt', 'on pyt', 'on pyt', 'on pyt', 'on']
>>> b.split('h',1)
['pyt', 'on python python python python']
>>> b.split('h',2)
['pyt', 'on pyt', 'on python python python']
>>>

2、字符串的拼接

第一种方法用加号"+"
>>> a
'hello'
>>> b
'python'
>>> c
'!'
>>> a+b+c
'hellopython!'
>>> a+''+b+ ''+c
'hellopython!'
>>> a+' '+b+' '+c
'hello python !'
>>>
第二种方法用格式化符号%s
>>> 'my name is %s' % b
'my name is python'
>>> 
>>> '%s %s %s' %(a,b,c)
'hello python !'
>>> 
>>> '%s i love %s %s' %(a,b,c)
'hello i love python !'
>>> 
第三种方法: join()
括号里是要连接的元组、列表

注意括号里是要连接的可以是列表,元组,
>>> m
['h', 'e', 'l', 'l', 'o']
>>> ''.join('m')
'm'
>>> ''.join(m)
'hello'
>>> L='I love Python'
>>> ''.join(L)
'I love Python'
>>> ' '.join(L)
'I l o v e P y t h o n'
>>> H=['hello','python','!']
>>> ''.join(H)
'hellopython!'
>>> ' '.join(H)
'hello python !'
>>>

>>> ' '.join([a,b,c])  (注:''里面是连接后面各个字符串的字符)

第四种方法: .format方式
>>> '{}{}{}'.format(a,b,c)
'hellopython!'
>>> 
>>> '{}{}{}'.format(a,b,c)
'hellopython!'
>>> '{2}{1}{0}'.format(a,b,c)
'!pythonhello'
>>> '{2}{0}{1}'.format(a,b,c)
'!hellopython'
>>> 
>>> '{x}{y}{z}'.format(x=a,y=b,z=c)
'hellopython!'
>>>

format方法详解:
'{}{}{}'.format(a,b,c)
当{}里面是空的时候,里面默认索引为0,1,2按format括号里的顺序依次填入
'{1}{2}{0}'.format(a,b,c)
当{}里面有索引值时,按前面的索引值将后面的每项依次填入
'{x}{y}{z}'.format(x=a,y=b,z=c)
{}里面可以指定对象名称,后面通过赋值的方式给前面的相应的值,后面是无序的

3、格式化字符串

格式化整数 %d
格式化小数 %f
>>> '%f' % 12
'12.000000'
>>> '%4s' % 'a'
' a'
>>> 
控制小数位数,4表示总长度
>>> '%10.2f' % 12
' 12.00'
>>> 
保证总长度为10位,其中小数有2位
>>> '%-10.2f' % 12
'12.00 '
>>> 
指定从那边开始,减号说明从左对齐,

%c格式化ASCII字符
>>> '%c' % 120
'x'
>>> 
%o格式化八进制
>>> '%o' % 8
'10'
%x格式化十六进制
>>> '%x' % 16
'10'
%e用科学计数法格式化
>>> '%e' % 1000
'1.000000e+03'
>>>

字符串转义
>>> x='abc\ndef'
>>> x
'abc\ndef'
>>> print(x)
abc
def
>>
直接输出和print()输出本质是不同的,print()输出中的end=\n

\\反斜杠
\'单引号
\"双引号
\n换行
\a提示音
\b 退格键
\r回车键
\t横向制表符
\f换页

原始字符r
表示不转义,使用真实字符
>>> c='abc\bde\taf\sa\ngeg'
>>> c
'abc\x08de\taf\\sa\ngeg'
>>> c=r'abc\bde\taf\sa\ngeg'
>>> c
'abc\\bde\\taf\\sa\\ngeg'
>>>

4、字符串常用的方法和属性

字符串的方法及属性:
s.count(x):返回字符串x在s中出现的次数,带可选参数
s.endswith(x):如果字符串s以x结尾,返回True
s.startswith(x):如果字符串s以x开头,返回True
s.find(x) :返回字符串中出现x的最左端字符的索引值,如果不在则返回-1
s.index(x):返回字符串中出现x的最左端的索引值,如果不在则抛出valueError异常
s.isalpha () :测试是否全是字母,中文,都是字母则返回 True,否则返回 False.
s.isdigit () :测试是否全是数字,都是数字则返回 True 否则返回 False.
s.islower () :测试是否全是小写
s.isupper () :测试是否全是大写
s.lower () :将字符串转为小写
s.upper () :将字符串转为大写 
s.replace (x,y) :子串替换,在字符串s中出现字符串x的任意位置都用y进行替换
s.split():返回一系列用空格分割的字符串列表
s.split(a,b):a,b为可选参数,a是将要分割的字符串,b是说明最多要分割几个

 

posted @ 2017-04-12 09:34  kindnull  阅读(185)  评论(0编辑  收藏  举报