1. Binary Representation

In Python, binary numbers are represented using the prefix 0b, followed by a sequence of binary digits (0 and 1). For example:

  binary_number = 0b1010
print(binary_number)  # Output: 10
  

Here, 0b1010 represents the binary number 1010, which converts to decimal 10.

2. Character Encoding

Character encoding in Python refers to the process of representing characters as numeric codes. The most common encoding is UTF-8, which supports a wide range of characters from various languages.

String Encoding and Decoding

Python provides methods to encode strings into bytes and decode bytes back into strings using specific encodings like UTF-8.

Encoding Example

  text = "Hello, 你好"
encoded_text = text.encode('utf-8')
print(encoded_text)  # Output: b'Hello, \xe4\xbd\xa0\xe5\xa5\xbd'
  

In this example:

  • The string text contains both English and Chinese characters.
  • The encode('utf-8') method converts the string into bytes using UTF-8 encoding.

Decoding Example

  encoded_text = b'Hello, \xe4\xbd\xa0\xe5\xa5\xbd'
decoded_text = encoded_text.decode('utf-8')
print(decoded_text)  # Output: Hello, 你好
  

In this example:

  • The decode(‘utf-8’) method converts the bytes back into a string using UTF-8 decoding.

Handling Different Encodings

Python also allows handling different encodings using the codecs module or specifying encoding parameters in file operations.

Reading and Writing Files with Encoding

  # Writing a file with UTF-8 encoding
with open('example.txt', 'w', encoding='utf-8') as file:
    file.write("Hello, 你好")

# Reading a file with UTF-8 encoding
with open('example.txt', 'r', encoding='utf-8') as file:
    content = file.read()
    print(content)  # Output: Hello, 你好
  

Here, the open() function is used with encoding=‘utf-8’ to ensure that the file is read or written using UTF-8 encoding.