Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python language normally

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use Python properly. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can learn something from the detailed introduction of this article.

Many users encounter various problems when using Python. I think Python is listed as one of the four programming languages hackers should learn, and I suggest that people start learning programming from Python.

Python has two default strings: str and unicode. When using Python, be sure to distinguish between "Unicode strings" and "unicode objects." All subsequent "unicode strings" refer to "unicode objects" in python.

In fact, there is no such thing as "Unicode strings" in Python, only "unicode" objects. A unicode string in the traditional sense can be represented exactly as a str object. Except at this point it's just a byte stream, which has no real meaning unless decoded as a unicode object.

You can see that on the console, Chinese characters are encoded GBK instead of UTF-16. Decode the string s (GBK encoding) to get an equivalent unicode object.

Note: Just because ss can be printed on the console does not mean it can be serialized directly, for example:

Directly outputting ss to a file throws the same exception. When dealing with unicode Chinese character string, you must first call encode function on it and convert it into other encoded output. This is the same for every environment. Summary: In Python, a "str" object is an array of bytes.

It doesn't matter whether the content is a valid string or what encoding (gbk, utf-8, unicode) the string uses. These content needs to be recorded and judged by the user himself. These restrictions apply to "unicode" objects as well. Keep in mind that the contents of a "unicode" object are not necessarily legal unicode strings.

We will see that soon. Summary: gbk encoded str objects and unicode encoded unicode objects are supported on windows consoles. In IDLE under Windows, the running effect is not exactly the same as the Windows console:

It can be seen that IDLE GBK encodes Chinese characters in character strings that are not identified by "u." However, for unicode strings using "u", IDLE actually uses GBK encoding, the difference is that at this time every character is unicode (object) character!! So len(ss) = 4.

This is how to use Python properly. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report