str or bytes although python and offer much the same functionalitythey package it differently in factthe mapping from to string types is not completely direct- ' str equates to both str and bytes in xand ' str equates to both str and unicode in moreoverthe mutability of ' bytearray is unique in practicethoughthis asymmetry is not as daunting as it might sound it boils down to the followingin xyou will use str for simple text and binary data and unicode for advanced forms of text whose character sets don' map to -bit bytesin xyou'll use str for any kind of text (asciilatin- and all other kinds of unicodeand bytes or bytearray for binary data in practicethe choice is often made for you by the tools you use--especially in the case of file processing toolsthe topic of the next section text and binary files file / (input and outputwas also revamped in to reflect the str/bytes distinction and automatically support encoding unicode text on transfers python now makes sharp platform-independent distinction between text files and binary filesin xtext files when file is opened in text modereading its data automatically decodes its content and returns it as strwriting takes str and automatically encodes it before transferring it to the file both reads and writes translate per platform default or provided encoding name text-mode files also support universal end-of-line translation and additional encoding specification arguments depending on the encoding nametext files may also automatically process the byte order mark sequence at the start of file (more on this momentarilybinary files when file is opened in binary mode by adding (lowercase onlyto the modestring argument in the built-in open callreading its data does not decode it in any way but simply returns its content raw and unchangedas bytes objectwriting similarly takes bytes object and transfers it to the file unchanged binary-mode files also accept bytearray object for the content to be written to the file because the language sharply differentiates between str and bytesyou must decide whether your data is text or binary in nature and use either str or bytes objects to represent its content in your scriptas appropriate ultimatelythe mode in which you open file will dictate which type of object your script will use to represent its contentif you are processing image filesdata transferred over networkspacked binary data whose content you must extractor some device data streamschances are good that you will want to deal with it using bytes and binary-mode files you might also opt for bytearray if you wish to update the data without making copies of it in memory string basics