How to Implement Transcoding in Python


In this example, we will learn how to implement transcoding in Python. The default encoding format in Python is ASCII format.

# -- coding: UTF-8 -- 
or 
#coding=utf-8

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

name=input('please input your name')


#.Convert the string to a utf-8 encoded byte and output it, then convert the byte to a utf-8 encoded string
print(name.encode("utf-8"))
print(name.encode("utf-8").decode("utf-8"))

# Convert the string to a gbk encoded byte and output it, then convert the byte to a gbk encoded string and output
print()
print(name.encode("gbk"))
print(name.encode("gbk").decode("gbk"))

print()
# The default encoding is utf-8
print(name)

Output:

please input your nameJohn Snow
b'John Snow'
John Snow

b'John Snow'
John Snow

John Snow
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments