Python built-in functions (61) -- str, python built-in 61str
English document:
Classstr
(Object ='')Class
str
(Object = B'',Encoding = 'utf-8',Errors = 'strict')
Return a string versionObject. IfObjectIs not provided, returns the empty string. Otherwise, the behaviorstr()
Depends on whetherEncodingOrErrorsIs given, as follows.
If neitherEncodingNorErrorsIs given,str(object)
Returnsobject.__str__()
, Which is the "informal" or nicely printable string representationObject. For string objects, this is the string itself. IfObjectDoes not have__str__()
Method, thenstr()
Falls back to returningrepr(object)
.
If at least oneEncodingOrErrorsIs given,ObjectShocould be a bytes-like object (e.g.bytes
Orbytearray
). In this case, ifObjectIsbytes
(Orbytearray
) Object, thenstr(bytes, encoding, errors)
Is equivalentbytes.decode(encoding, errors)
. Otherwise, the bytes object underlying the buffer object is obtained before callingbytes.decode()
. See Binary Sequence Types-bytes, bytearray, memoryview and Buffer Protocol for information on buffer objects.
Note:
1. The str function converts an object to its string representation. If no parameter is input, an empty string is returned.
>>> str()''>>> str(None)'None'>>> str('abc')'abc'>>> str(123)'123'
2. When converting a binary stream, you can input the encoding parameter to indicate the encoding format used to read the byte array. The errors parameter indicates the error level for reading binary data. (The two parameters have the same value and similar meanings as the parameters of the same name in the open method. For details, see Python built-in function (47) -- open ).
>>> File = open('test.txt ', 'rb') # open a file >>> fileBytes = file. read () # Read Binary streams> fileBytesb '\ xe6 \ x88 \ x91 \ xe6 \ x98 \ xaf \ xe7 \ xac \ xac1 \ xe8 \ xa1 \ x8c \ xe6 \ x96 \ x87 \ xe6 \ x9c \ xac \ xef \ xbc \ x8c \ xe6 \ x88 \ x91 \ xe5 \ xb0 \ x86 \ xe8 \ xa2 \ xab \ xe6 \ x98 \ xbe \ xe7 \ xa4 \ xba \ xe5 \ x9c \ xa8 \ xe5 \ xb1 \ x8f \ xe5 \ xb9 \ x95 \ r \ n \ xe6 \ x88 \ x91 \ xe6 \ x98 \ xaf \ xe7 \ xac \ xac2 \ xe8 \ xa1 \ x8c \ xe6 \ x96 \ x87 \ xe6 \ x9c \ xac \ xef \ xbc \ x8c \ xe6 \ x88 \ x91 \ xe5 \ xb0 \ x86 \ xe8 \ xa2 \ xab \ xe6 \ X98 \ xbe \ xe7 \ xa4 \ xba \ xe5 \ x9c \ xa8 \ xe5 \ xb1 \ x8f \ xe5 \ xb9 \ x95 \ r \ n \ xe6 \ x88 \ x91 \ xe6 \ x98 \ xaf \ xe7 \ xac \ xac3 \ xe8 \ xa1 \ x8c \ xe6 \ x96 \ x87 \ xe6 \ x9c \ xac \ xef \ xbc \ x8cr \ xe6 \ x88 \ x91 \ xe5 \ xb0 \ x86 \ xe8 \ xa2 \ xab \ xe6 \ x98 \ xbe \ xe7 \ xa4 \ xba \ xe5 \ x9c \ xa8 \ xe5 \ xb1 \ x8f \ xe5 \ xb9 \ x95' >>> str (fileBytes) # By default, the binary stream is converted into the string representation "B '\ xe6 \ x88 \ x91 \ xe6 \ x98 \ xaf \ xe7 \ xac \ xac1 \ \ xe8 \ xa1 \ x8c \ xe6 \ x96 \ x87 \ xe6 \ x9c \ xac \ xef \ xbc \ x8c \ xe6 \\ x88 \ \ X91 \ xe5 \ xb0 \ x86 \ xe8 \ xa2 \ xab \ xe6 \ x98 \ xbe \ xe7 \ xa4 \ xba \\ xe5 \ x9c \ xa8 \ xe5 \ xb1 \ x8f \ xe5 \ xb9 \ x95 \ r \ n \ xe6 \ x88 \ x91 \ xe6 \ x98 \ xaf \ xe7 \ xac \ xac2 \ xe8 \ xa1 \ x8c \ xe6 \ x96 \ x87 \ xe6 \ \ x9c \ xac \ xef \ xbc \ x8c \ xe6 \ x88 \ x91 \ xe5 \ xb0 \ x86 \ xe8 \ xa2 \\ xab \ xe6 \ x98 \ xbe \ xe7 \ xa4 \ xba \ xe5 \ x9c \ xa8 \ xe5 \ xb1 \ x8f \ xe5 \ xb9 \ x95 \ r \ n \ xe6 \ x88 \ x91 \ xe6 \ x98 \ xaf \ xe7 \ xac \ xac3 \ \ xe8 \\ Xa1 \ x8c \ xe6 \ x96 \ x87 \ xe6 \ x9c \ xac \ xef \ xbc \ x8cr \ xe6 \ x88 \ x91 \ xe5 \ xb0 \ x86 \ xe8 \ xa2 \ xab \ xe6 \ x98 \ xbe \ xe7 \ xa4 \ xba \ xe5 \ \ x9c \ xa8 \ xe5 \ xb1 \ x8f \ xe5 \ xb9 \ x95 '">>> str (fileBytes, 'utf-8') # input the encoding parameter. The function will read the binary stream content using this encoding. 'I am a 1st line text, I will be displayed on the screen \ r \ n I am 2nd lines of text, I will be displayed on the screen \ r \ n I am 3rd lines of text, r I will be displayed on the screen '> str (fileBytes, 'gbk') # An error will be reported when the input encoding cannot be decoded (that is, the default errors parameter is strict) traceback (most recent call last): File "<Pyshell #46>", line 1, in <module> str (fileBytes, 'gbk') UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 8: illegal multibyte sequence >>> str (fileBytes, 'gbk', 'ignore') # 'ignore' ignore level, character encoding error, ignore. '{}}}{{{{}}{{ ㄥ \ r \ n 2 2 2 why are there too many? \ r \ n numbers? Why? '> str (fileBytes, 'gbk', 'replace ') # replace level. If the character encoding is incorrect, replace it ?. "Why ?" why? TV drama'