5. python string, 5. python string

Source: Internet
Author: User

5. python string, 5. python string

I mentioned the word "string" before and now I will learn what a string is.

First, the string is the built-in data type of python. It is characterized by quotation marks and can be single quotation marks ('string') or double quotation marks ("string "), three quotation marks (''' string ''' and "string """). Note that these symbols areHalfwidth symbolAnd it is an English quotation mark, that is, the quotation mark in the Chinese input method cannot be used.

These strings areSeparate useAnd are bothOne rowThere is no difference, for example:

a = '123'b = "123"c = '''123'''d = """123"""

 

These four types of writing are one of the meanings. In addition, note that all the quotation marks are of the string type, even if they are numbers or other data types. Therefore, when adding a string, the result is not a mathematical addition, but a string'sSplicing.

As shown above, but we generally do not splice strings in this way, because the splicing method here is very inefficient, wasting memory space. For example:

a = '123' + '456'

 

In this case, '123456' is created in the memory, '123456' is created, spliced into '123456', and placed in the memory. Then, the variable points to the value in the memory. In this process, we create two additional values, which we do not need. If a large amount of data is spliced, a large amount of useless data will be generated, which will not only waste memory, but also trigger python's garbage collection mechanism. python does not do anything else during garbage collection, in this way, our code efficiency will be very low.

 

1. String concatenation

In addition to using the + sign for splicing, there are also two ways to achieve splicing (In fact, there is another use+ =The concatenation method of symbols seems to be efficient, but I also read other people's articles. I am not very clear about the principle, so I won't talk about it anymore.).

1. 'IO. StringIO ', memory string io concatenation (from the memory level, there are very few operations, and I have not learned much about it. I will not explain it here)

2. str. join (), using the string join () method (str is the class used to create string objects. It will be explained in detail when it comes to object-oriented. Here we will see the code demonstration first)

A = ''# create an empty string first, which is called object a according to the object-oriented statement. join ('abc', 'def ') # concatenate a string using the join () method, while the join () method inherits from the parent class.

 

Here I added the explanation from object-oriented. If I couldn't understand it, I could come back after I finished Learning Object-oriented. When I was studying it, I was also overwhelmed, but after learning object-oriented, I have a feeling of epiphany. I will share some of my understanding about object orientation in the future.

To sum up its usage:

1. First, you must have a string object created by the python built-in class str; otherwise, no join () exists.

2. the syntax format is str. join (sequence), where sequence represents a sequence. There are six built-in sequences in Python: List, tuples, strings, Unicode strings, buffer objects, and xrange objects (which will be summarized later ).

That is to say, in addition to passing a ancestor as in the example, the string can also be passed, can it be written like this:

b = ''b.join('123','456')

 

The answer is no. An error is reported. The error message is as follows:

Specifically, the function only requires one data, but we give two, so an error is returned.

That is to say, when we write it like this, we actually pass in two sequences. A string is a sequence.

So what if we pass only one string?

It seems that there is no problem. What if B is not empty?

This is different from what we think. It is not directly spliced at the end. Obviously, this is not what we want (Potholes, this is) What if it is the ancestor?

(Note: join () does not change the value of B, but returns a new object. The return value will be discussed in the function)

Well, it's also a big pitfall.

  It seems that after the input sequence, the original strings are inserted between the values taken one by one based on the index.

Coach, this is different from what I said!

In fact, we have a better method, that is, string formatting.

 

 2. String formatting

The principle of string formatting is that I occupy a hole in it first, but I will not fill this hole for the time being. When I want to use it, I will throw the stuff to be filled in.

First look at the sample code:

Name = 'scolia 'a = '% s is a handsome guy' % nameprint

 

Scolia uses string formatting to pull the results. scolia is named 'handsome guy.

% S is the pitfall, while % name is what we want to fill in. Of course, we usually useVariableTo store this value. If you dynamically change the name value in combination with user interaction, then everyone can be a handsome guy.

But we are not satisfied yet. What if the user is a female? It's not suitable to use handsome guys. At this time, we can take up to several pitfalls.

Name = 'scolia 'how = 'handsome guy 'a =' % s is a % s' % (name, how) print

 

Result:

No problem. You can take up a few pitfalls, but the writing style is a little changed (changed to the form of the ancestor, which will be detailed later). At this time, you can change it as you want (Write by yourself).

Theoretically, several placeholders can be used, as long as there is a corresponding variable (the number of variables does not correspond to the number of placeholders will report an error), and they are replaced by the placeholder in order.

But what if we don't want to upload data in order? You can write as follows:

A = '% (name) s is a % (how) s' % {'who': 'handsome guy', 'name': 'scolia '}

 

The result is the same as the above. Use the dictionary to pass the value, and then use the dictionary key to obtain the value. The dictionary details will be discussed later.

At this time, some people will ask what s means in % s (When someone asks this question, don't speak out.), Here s indicates that I will use a string to fill in this pitfall. Similarly, we can also write the following code:

Format Description
% S String
% R String (in repr () Format)
% D Signed INTEGER (decimal)
% U Unsigned integer (decimal)
% O Unsigned integer (octal)
% X Unsigned integer (hexadecimal)
% X Unsigned integer (hexadecimal uppercase characters)
% E Floating Point Number (Scientific Notation)
% E Floating Point Number (Scientific notation, represented by e)
% F Floating Point Number (decimal point)
% G Floating Point Number (% e or % f based on the value size)
% G Floating Point Number (similar to % g)
% P Pointer (memory address for printing value in hexadecimal format)
% N Store the number of output characters in the next variable in the parameter list
% C Character and ASCII code
% Character '%'

I found this table from the Internet. There is ...... Are there many types? Are there any placeholders I need to replace?

First, these things control the display format. In many cases, % s is enough, because when this tag is used, the incoming data is first converted to a string using the str () method, all data types in python can be converted to strings (written in this way by others), and the final output result is a string (otherwise, how can it be called string formatting ). In most cases, there is no problem.

Therefore, the placeholder format can be summarized:

%[(name)][flags][width].[precision]typecode

 

[] Indicates optional, which will be expressed later.

(Name): use the dictionary key to pass the value, as mentioned above

Typecode: data type, which is also the ones in the preceding table. Generally, % s is enough. Of course, other types can be used.

Flags: can include +,-, '', or 0. + Indicates the right alignment. -Indicates left alignment. ''Is a space. 0 indicates filling with 0.

Width: indicates the display width. After the width is set, the padding effect can be achieved only by alignment above.

What do these alignment, fill, and width mean? First of all, I have a string 'scolia 'with a length of 6, but I have to use a length of 10 characters to show what to do. Is there four more positions, what can we do with these four positions? Can we fill them with spaces or 0. But are there any uncertainty about the four-plus length positions? I can let the original characters occupy the positions first, that is, the so-called alignment. The remaining positions after alignment are the filling positions.

Precision: indicates the precision after the decimal point. It can be used with floating point numbers.

 Of course, there are other methods for string formatting, which will not be further explored here.

 

3. Nesting of quotation marks

As mentioned at the beginning, when several quotation marks are used separately and only one row is used, the effects are the same. However, when using nested functions, you must pay attention to some issues. See the following example:

A = 'scolia says: 'I want to say a word.' # incorrect syntax

At this time, it will cause confusion. Just like the color logo here, the previous section is treated as a string, and the subsequent section does not conform to the syntax, and an error is certainly returned.

So, this is the case now.

A = 'scolia says: \ 'I want to say one sentence \''
Print

Here, \ is an escape character. Here, it converts meaningful quotation marks (') into common symbols, and the meaning of the string is lost after being escaped.

The result is what we want, but it is too troublesome to escape every time. At this time, we can do this.

A = "scolia says, 'I want to say something '"
Print

Use double quotation marks on the periphery and single quotation marks on the inside, so that the single quotation marks on the inside will not be interpreted as strings to be created.

Of course, this is also possible:

A = 'scolia said: "I want to say a word." 'print

The same is true.

However, sometimes we have the following requirements:

A = 'scolia said: "I want to say a word. This sentence is long, long"

The result is as follows:

But this sentence is too long. I want to wrap it like this?

So write:

A = 'scolia said: "I want to say a word, this sentence is long" print

The result is as follows:

Here, \ n refers to the line break (of course, the \ here also escape characters, but it turns meaningless characters into meaningful ones), and a line break will appear here, there are also many special characters, which are not listed one by one here.

However, it is too troublesome to use \ n for each line feed. Can't I wrap it myself when I write it?

Obviously, my IDE uses a red wavy line to indicate that this is a syntax error.

Write it like this

The \ symbol indicates that this line of code cannot be written. I need to wrap the line and write it again (this symbol is the same as that in linux commands). The result is:

Hmm ...... If there are a few more spaces, I will change them again:

Run:

Very good. The space is gone. However, there is nothing to use.

Don't you want to solve it? At this time, the three quotation marks indicate: child, you are too young.

A = ''' scolia said: "I want to say a word, this sentence is long term" ''' print

Pay attention to the nesting of single double quotation marks.

It's a magic to see the running results.

At this time, the wise person said that the three quotation marks are not annotated with multiple lines? Why can it be used as a special symbol to create a string!

In fact, there is an excellent way to tell whether the three quotation marks are written in a comment or a string, that is, to see whether there is a value assigned to a variable before it, and to see the standard value assignment symbol (= ). If there is a value assignment, it is to create a String object, no, it is a comment.

  It's really wit like me, and I found it all.

Now, let's talk about the quotation marks first.

4. String sequence characteristics

The string is also a sequence, so you can use the sequence method to process it.

All sequence types can perform certain operations. These operations include: Index, sliceing, adding, multiplying, and checking whether an element belongs to a Sequence member (membership ). In addition, Python also provides built-in functions for calculating the sequence length and finding the maximum and minimum elements.

The sequence will be explained in another article in the future. If you are in a hurry, you can see how others write the sequence first, or you can see that I have updated the subsequent content.

 

5. built-in string Methods

There are a bunch of these methods, and some may be hard to remember, but I suggest you review the content after learning object-oriented, and then there will be an epiphany.

The summary of the built-in methods should be explained in another article, because it is too long and long to write here.

Let me take a break ......

 

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.