Python built-in function (--STR)

Source: Internet
Author: User

English documents:

class str (object= ") class

str(object=b ', encoding= ' Utf-8 ', errors= ' strict ')

Return A string version of object. If Object is not provided, returns the empty string. Otherwise, the behavior of str() depends on whether encoding or errors are given, as follows.

If neither encoding nor errors is given, str(object) returns object.__str__() , which is the "informal" or nicely printabl E string representation of object. For string objects, this is the string itself. If Object does not has a __str__() method, then str() falls back to returning repr(object) .

If at least one ofencodingOrErrorsis given,Objectshould be aBytes-like Object(e.g.bytesOrbytearray). In the case, ifObjectis abytes(orbytearray) object, thenstr(bytes, encoding, errors)is equivalent tobytes.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.

Description

1. The STR function functions when the object is converted to its string representation, and an empty string is returned if the parameter is not passed in.

>>> str ()' >>> str (none)'none'>> > str ('abc')'abc'>>> str (123 )'123'

2. When converting a binary stream, you can pass in the parameter encoding, which indicates the encoding format used to read the byte array, and the parameter errors, which indicates the error level of the read binary. (These two parameters have the same value and similar meanings as those in the open method, see Python's Built-in function (--open).)

>>> file = open ('Test.txt','RB')#Open File>>> filebytes = File.read ()#Read binary stream>>>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, binary flow is converted to 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')#passing in the encoding parameter, the function reads the contents of the binary stream with this encoding'I am the 1th line of text, I will be displayed on the screen \ r \ n I am the 2nd line of text, I will be displayed on the screen \ r \ n I am the 3rd line of text, R I will be displayed on the screen'>>> Str (filebytes,'GBK')#An error occurs when an incoming encoding cannot be decoded (that is, the errors parameter defaults to strict)Traceback (most recent): File"<pyshell#46>", Line 1,inch<module>Str (filebytes,'GBK') Unicodedecodeerror:'GBK'Codec can'T decode byte 0xac in position 8:illegal multibyte sequence>>> Str (filebytes,'GBK','Ignore')#' Ignore ' ignores levels, character encodings are wrong, ignored.'Contact Language Ionizing Quilting 1 Aachen 屾 PO chain 锛 屾 An Hao oooo display drama identify 鍦-Hi Caroline Qian \ r Contact Language ionizing Quilting 2 Aachen 屾 PO chain 锛 屾 An oooo display identify play 鍦-hi Caroline \ r Qian Contact language Quilting 3 Aachen ionizing 屾 chain PO 锛 宺 Contact led 皢 drama 琚鏄 identify 鍦-Hi Caroline'>>> Str (filebytes,'GBK','Replace')#' replace ' replacement level, character encoding error, replace with?.'Contact Language Ionizing Quilting 1 Aachen 屾 po chain? 锛 屾 an Hao oooo?? Display drama identify 鍦-Hi Caroline qian? \ r \ n Contact language ionizing Quilting 2 Aachen 屾 po chain? 锛 屾 an Hao oooo?? Display drama identify 鍦-Hi Caroline qian? \ r \ n Contact language ionizing Quilting 3 Aachen屾 po chain? 锛 宺 Contact led 皢 Kui? display drama identify 鍦-Hi Caroline Qian?'

Python built-in function (--STR)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.