码迷,mamicode.com
首页 > 编程语言 > 详细

python 字符串

时间:2019-06-24 17:06:05      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:uid   slow   attr   api   操作方法   串操作   字符   str   ack   

key point:  字符串都是不可改变的

 

打印:

>>> values
(‘world‘, ‘hot‘)
>>> format
‘hello, %s. %s enough for ya‘
>>> print(format % values)
hello, world. hot enough for ya

 

>>> ‘%s plus %s equals %s‘ % (1, 1, 2)
‘1 plus 1 equals 2‘

 

>>> ‘Using str %r‘ % 42
‘Using str 42‘

 

>>> ‘Using str %r‘ % 42
‘Using str 42‘
>>> ‘%10f‘ %pi
‘ 3.141593‘
>>> ‘%10.2f‘ % pi
‘ 3.14‘
>>> ‘%.2f‘ % pi
‘3.14‘
>>> ‘%.5s‘ % ‘Guido van rossum‘
‘Guido‘

>>> ‘%010.2f‘ % pi
‘0000003.14‘

 

字符串操作方法:find

>>> title = "Monty Python‘s Flying Circus"
>>> title.find(‘Python‘)
6

 

字符串操作方法:join

>>> ‘+‘.join([‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘])
‘1+2+3+4+5‘
>>> dirs = ‘‘, ‘usr‘,‘bin‘,‘env‘
>>> ‘/‘.join(dirs)
‘/usr/bin/env‘

 

字符串操作方法: lower title capitalize swapcase

>>> ‘test‘.islower()
True
>>> ‘test‘.titla()
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: ‘str‘ object has no attribute ‘titla‘
>>> ‘test‘.title()
‘Test‘
>>> ‘test‘.capitalize()
‘Test‘
>>> ‘test‘.swapcase()
‘TEST‘

 

字符串操作方法:replace

>>> ‘THis is a test‘.replace(‘is‘,‘eez‘)
‘THeez eez a test‘

 

字符串操作方法: split

>>> ‘1+2+3+4+5‘.split(‘+‘)
[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]

 

字符串操作方法:strip()

>>> ‘ pan is pan ‘.strip()
‘pan is pan‘

 

 

字符串操作方法:translate

 

python3.7 not support

 

python 字符串

标签:uid   slow   attr   api   操作方法   串操作   字符   str   ack   

原文地址:https://www.cnblogs.com/lianghong881018/p/11077617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!