Talking about the python string storage form, talking about the python string

Source: Internet
Author: User

Talking about the python string storage form, talking about the python string

Http://blog.csdn.net/zhonghuan1992

Zhongkui

August 31, 2014



Python string Storage Format



Record your questions found today and give answers to your existing knowledge. To put it short, python users should be familiar with = and is, But I will introduce it here.

= Is used to determine whether two things are equal, for example:

a = 10;b = 10;print(a == b);

The output is true;

Let's look at another example:

a = [1,2,3];b = [1,2,3];c = [1,2,4];print(a==b);print(a==c);

The output values are true and false respectively;


Let's look at is; it is used to judge the same identity, not the same sex.

Let's look at the following example:

x = [1,2,3];y = [1,2,3];print(x is y);

The output is false;


And only:

x = [1,2];y = x;print(x is y);

The output is true;


From the above example, we can see the difference between = and is.

Now, if we replace the comparison with a string, will the result be unexpected. See the following example:

x = “huan”;y = x;print(x == y);print(x is y);

I think you can guess what the output is.

The output is true.

True


Now let's look at the following example:

x = “huan”;y = “huan”;print(x == y);print(x is y);

What are the results?

The output is true.

True

Why? It is understandable that x and y are equal, but why is x and y the same object? According to my guess, the python internal storage string, using the enjoy meta mode (Flyweight pattern), what is enjoy meta mode, can see a blog I wrote a few days ago, http://blog.csdn.net/zhonghuan1992/article/details/38856591,

The enjoy mode is an optimization method to reduce the storage space. After the above discussion, we know that the two strings with the same string content actually point to the same object. This is exactly what the meta mode does, therefore, I guess the internal implementation mechanism is based on this mode.

So why can we use this mode? If you have learned python, you should understand that strings belong to the unchanged type in python, and are the same as tuple. For this reason, strings can adopt the meta mode.

This is my current understanding. If you have other opinions, please leave your opinions for discussion.



Wrap python strings in the list

You can use '\ n' to separate the string to obtain a list.
For example, if your input string contains the test_string variable, test_string.split ('\ n') is a list with three elements. Each element is a string (without line breaks)

Format python strings

In python, the output tag is similar to the printf () format in c. In python, the % operator is used to format the output string. The common format is
Format mark string % value group to be output
The "format mark string" on the Left can be exactly the same as that in c. If there are two or more values in the 'value Group' on the right, they must be enclosed in parentheses and separated by short signs. Focus on the left part. The simplest form of the left part is:
% Cdoe
There are many types of codes, but in python, everything can be converted to the string type. Therefore, if there are no special requirements, you can use '% s' to mark them all. For example:
'% S % s' % (1, 2.3, ['one', 'two', 'three'])
Its output is '1 2.3 ['one', 'two', 'three] ', which is output according to the mark on the left of %. Although the first and second values are not of the string type, there is no problem. In this process, when the computer finds that the first value is not % s, it will first call the integer function to convert the first value, that is, 1, to the string type, then, call the str () function to output the data. As mentioned above, there is also a repr () function. If you want to use this function, you can mark it with % r. In addition to % s, there are many similar codes:
Integer: % d
Unsigned integer: % u
Octal: % o
Hexadecimal: % x % X
Floating Point: % f
Scientific Note: % e % E
Automatically select % e or % f: % g based on different values
Automatically select % E or % f: % G based on different values
As mentioned above, escape with "\" is the same. Here, % is used as the mark of the format, and there is also a question about how % should be output. If you want to output % itself in the format mark string, % can be used.
The above is just the simplest form of format mark. It is more complex:
'% 6.2f' % 1.235
In this form, a decimal point 6.2 is displayed before f, which indicates that the total output length is 6 characters, with two decimal places. More complex:
'% 06.2f' % 1.235
A value of 0 is added before 6, indicating that if the number of output digits is less than 6, 0 is used to make up 6 digits. The output of this row is '001. 24'. It can be seen that the decimal point also occupies one place. Tags similar to 0 include-and +. -Indicates the left alignment, and + indicates that the plus sign is also placed before the positive number. By default, the plus sign is not added. Finally, let's look at the most complex form:
'% (Name) s: % (score) 06.1f' % {'score': 9.5, 'name': 'newsim '}
In this form, only when the output content is dictionary (a python data type), the (name) and (score) in parentheses correspond to the keys in the subsequent key-value pairs. In the previous example, we can see that the order marked in the "format mark string" and the values in the "value group to be output" are one-to-one, ordered, one-to-one, and two-to-two. In this form, no value corresponding to each format mark is specified by the key in parentheses. The output of this line of code is: 'newsim: 808080 '.
 
Sometimes in the form of % 6.2f, 6 and 2 cannot be specified in advance and will be generated during the program running. How can we input this? Of course, % d cannot be used. % df or % d. % d % f. It can be in the form of % *. * f. Of course, the following "value group to be output" contains the two * values. For example, '% *. * F' % (6, 2, 2.345) is equivalent to' % 6.2f '% 2.345.
This is the most complex content this book has ever seen. However, if you cannot remember, or... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.