input
- raw_input()
在py2版本中,raw_input接收輸入流並直接轉換存儲爲string類型
- input()
py2版本中,通過根據輸入類型不同存儲爲不同格式。
比如像輸入string,需在內容外包裹單引或者雙引
py3整合input與raw_input,將所有輸入作爲string保存
py2中print
不是將print作爲一個方法函數,二十當做語句使用,調用時不能再後面直接()
Ps:同樣的還有exec語句轉換成了exec()
str=
repr
Python 有辦法將任意值轉爲字符串:將它傳入repr() 或str() 函數。
函數str() 用於將值轉化爲適於人閱讀的形式,而repr() 轉化爲供解釋器讀取的形式(如果沒有等價的語法,則會發生SyntaxError 異常) 某對象沒有適於人閱讀的解釋形式的話,str() 會返回與repr()等同的值。很多類型,諸如數值或鏈表、字典這樣的結構,針對各函數都有着統一的解讀方式。字符串和浮點數,有着獨特的解讀方式。
str直接將object轉換成string
repr則更類似於Java中的toString()
,將相關類型內容顯示
unicode與str
unicode、utf-8、ascii編碼等相關前置知識可以瀏覽TYuenCN博客
ANSI是默認的編碼方式。對於英文文件是ASCII編碼,對於簡體中文文件是GB2312編碼(只針對Windows簡體中文版,如果是繁體中文版會採用Big5碼)
str和unicode都是basestring的子類,而實際上,str是字節串,由unicode經過編碼(encode)後的字節組成。unicode纔是真正意義上的字符串,由字符組成
下面是一個基本的轉換流程
str -> decode('the_coding_of_str') -> unicode
unicode -> encode(‘the_coding_you_want‘) -> str
decode
The method decode() decodes the string using the codec registered for encoding. It defaults to the default string encoding.
#!/usr/bin/python
Str = "this is string example....wow!!!";
Str = Str.encode('base64','strict');
print "Encoded String: " + Str
print "Decoded String: " + Str.decode('base64','strict')
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!
encode
The method encode() returns an encoded version of the string. Default encoding is the current default string encoding. The errors may be given to set a different error handling scheme.
#!/usr/bin/python
str = "this is string example....wow!!!";
print "Encoded String: " + str.encode('base64','strict')
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=