Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

Python Bytestring क्या है?


Python Bytestring

एक String Characters का Sequence होता है; ये एक Abstract Concept हैं और इन्हें Directly Disk पर Store नहीं किया जा सकता। एक Byte String Bytes का Sequence होती है - ऐसी चीजें जो Disk पर Store की जा सकती हैं। इनके बीच Mapping को Encoding कहते हैं - इनमें से काफी सारे हैं (और अनंत संभव हैं) - और Conversion करने के लिए आपको पता होना चाहिए कि Particular Case में कौन सा Apply होता है, क्योंकि Different Encoding Same Bytes को Different String में Map कर सकती है।

उदाहरण: Same Bytes, Different Encodings

>>> b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'.decode('utf-16')
'蓏콯캁澽苏'
>>> b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'.decode('utf-8')
'τορνoς'

एक बार जब आप जान जाते हैं कि किस Encoding का उपयोग करना है, तो आप Byte String की .decode() Method का उपयोग करके उससे सही Character String प्राप्त कर सकते हैं। Character String की .encode() Method Opposite Way जाती है और Character String को Byte String के रूप में Encode करती है।

Key Concepts:

Concept Type Methods
String (Text) str .encode()
Byte String (Binary) bytes .decode()

Common Encodings:

  • UTF-8: Most Common, Variable Width, ASCII Compatible
  • UTF-16: 2 या 4 Bytes Per Character
  • ASCII: केवल 128 Characters, Legacy
  • Latin-1 (ISO-8859-1): Western European Languages

Best Practices:

  • Text के लिए हमेशा str उपयोग करें
  • I/O (Files, Network) के लिए bytes उपयोग करें
  • Explicit Encoding Specify करें: .encode('utf-8')
  • Errors Handle करें: .decode('utf-8', errors='replace')

  1. - Python में zfill() Method क्या है?

    zfill() Method: String में Leading Zeros Add करना zfill Method String में Left Side Zeros Pad करने के लिए बनाया गया है। उदाहरण >>> 25.zfill(6) 000025 Alternative: rjust() Method हम String Class के rjust(width[, fillchar]) Method का भी उपयोग कर सकते हैं जो String को Right Justify करता है

  1. - Python String की Max Length क्या है?

    Python String की Maximum Length 64-bit Python Installation और 64 GB Memory के साथ, लगभग 63 GB की Python 2 String काफी Feasible होनी चाहिए। यदि आप अपनी Memory को इससे कहीं अधिक Upgrade कर सकते हैं, तो आपकी Maximum Feasible Strings आनुपातिक रूप से Longer होनी चाहिए। लेकिन यह Runtimes पर Hit के साथ आ

  1. - Python में String और Byte String में क्या Difference है?

    String vs Byte String एक String Characters का Sequence होता है; ये एक Abstract Concept हैं और इन्हें Directly Disk पर Store नहीं किया जा सकता। एक Byte String Bytes का Sequence होती है - ऐसी चीजें जो Disk पर Store की जा सकती हैं। इनके बीच Mapping को Encoding कहते हैं - इनमें से काफी सारे हैं (और अनं