Home | Mirror | Search | 杂文 | ITEYE 博客 | OSChina 博客 | 51CTO 博客

3.2. String

3.2.1. String function

3.2.1.1. str.find()

找到字符,返回字符串位置。没有找到返回 -1

"aaa bbb ccc".find('aaa')
				

3.2.1.2. str.find()

查找并替换字符串

a = 'hello word'
a.replace('word','python')
				

3.2.2. Convert str to bytes in python

			
>>> b = str.encode(y)
>>> type(b) >>> b b’Hello World!’
To alter from bytes to str, capitalize on bytes.decode().
>>> z = b”Hello World!”
>>> y = “Hello World!”
>>> type(z)

>>> type(y)

To alter from str to bytes, capitalize on str.encode().
>>> a = bytes.decode(z)
>>> type(a)

>>> a
‘Hello World!’
			
			

3.2.3. String format

strHello = "the length of (%s) is %d" %('Hello World',len('Hello World')) print strHello

comments powered by Disqus