The Python command line is widely used. We have many difficulties when using it. Let's take a detailed look at how to solve these problems. I hope you will be helpful in later use.
When I was a beginner in Python command line and encountered many coding problems, I wrote it down so that I could not forget it in the future. Many things do not understand it. It was not difficult to understand and draw conclusions. However, when I thought of it, I could help some enthusiastic people to point out the error, then you have a thick face ......
First, you need to know that Python has two types of strings (strictly speaking, this does not seem to be the case ). One is an ordinary str object, each character is represented by 8 bits), the other is a unicode string, they can be converted to each other.
Open pyshell and enter a piece of code.
Python code
- >>> A = "I"
- >>> B = unicode (a, "gb2312 ")
- >>> A. _ class __
- <Type 'str'>
- >>> B. _ class __
- <Type 'unicode '>
- >>>
See the two strings. Come back
Python code
- >>> a
- '\xce\xd2'
- >>> b
- u'\u6211'
Variable a is a string of two characters, and variable B is a unicode character. For The two types of Strings, The Python documentation --> javasagereference --> DataModel --> The standard type hierarchy --> Sequences has some Strings and Unicode descriptions. As
Python command line code
- >>> Z = u "I"
- >>># This type of code is actually nothing.
- >>> Z. _ class __
- <Type 'unicode '>
- >>> Z
- U' \ xce \ xd2'
You see, this strange thing ...... later, I tried it in Windows XP and python command lines. Different conclusions are drawn. The result of z is changed to U' \ u6211 '. Here we should not try it in pyshell, it seems that there are still many questions that have not been clearly understood