Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

4.2. String

4.2.1. String function

4.2.1.1. str.find()

找到字元,返回字元串位置。沒有找到返回 -1

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

4.2.1.2. str.find()

查找並替換字元串

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

4.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!’


# to utf-8		
'BG7NYT'.encode('utf-8')
# to utf-16
'BG7NYT'.encode('utf-16')
			
			

4.2.3. String format

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