This is the 16th day of my participation in Gwen Challenge

To be a happy man, reading, travel, hard work, care for the body and mind. Be a happy person, read, travel, work hard, care about the body and mind.

Far away, still far away...

Earlier we talked about writing temperature converters in Python, drawing “Python”, and numeric types. I do not know whether you still remember, forget you can click on the album to review inside oh, you can also knock on the code, improve the proficiency of the sentence.

After learning Python, you will find that Python can do a lot of high quality things, such as crawlers, data analysis, machine learning, game development, etc., all of which are based on Python, so if you want to use Python well, you must lay a solid foundation of basic knowledge, and string is the key of the foundation. So today we are going to learn the syntax of strings.

First, the editor divides the string into four blocks, namely string types, string operators, string handlers, and string handler methods.

The value is a string

We’ve talked a little bit about strings in C, but there’s a big difference between C and Python, and Python provides a little bit more functionality than C.

Here we begin with the concept of a concord string: an ordered sequence of zero or more characters, represented by a pair of single or double quotation marks, as in output:

Print ("Hello,world") or print('C').Copy the code

Since a string is an ordered sequence of characters, it is possible to index the characters in it. Emmm temperature converter (emMM) temperature converter (emMM) temperature converter (EMMM) temperature converter

Slice: the form is < string >[M:N],M missing means to the beginning, N missing means to the end, for example: "0123456789" [:3] result is "012"Copy the code

Advanced slice has one more slice for the step size, in the form of < string >[M:N:K]. This is one more variable than we learned before, which is K. From M to N, print characters with step size K.

"0123456789"[::-1] result is "987654321"Copy the code

If you want to print double or single quotation marks, you can use the corresponding single or double quotation marks. For example: Print (‘ here is a double quote ‘”‘), you can try it more often, after all, if you really want to learn programming well, you can’t learn it without typing tens of thousands of lines of code. It is worth noting that we played a Python game when we first got into Python. There was an example of a triple double quotation mark, which can represent a multi-line string, as follows: Life is short, I used Python’s little game

Python also provides a feature for string representation (reverse ordering) that is not available in C, so it would take several lines of code in C to do it. A simple way to express reverse order:

"0123456789"[::-1] result is "987654321"Copy the code

Finally, there is the escape character \, which is used to express the meaning of certain characters, such as “for double quotes. Of course, \b also has other uses. For example, \b stands for fallback. In the following code example, the line disappears when the output is printed because the cursor falls back one bit when the output is printed, meaning that the following character obscures the previous character.

Print (‘ line \b ‘line will disappear ‘)

String operators (three)

With regard to string operators, there are three more commonly used operators:

X +y: Joins two strings x and y. There are places where this join method doesn’t work very well, how should I say, for example, when we’re writing crawlers to join urls to certain strings, we usually use URlJoin.

N *x or x*n: Copies the string x (n is a number) n times, and is rarely used in ordinary times.

X in S: Returns True if x is a string of S, False otherwise (in is a reserved word) to determine whether x is in S.

String handling functions (six)

Since it is a string processing function, so here is a small editor for you to sort out the commonly used six functions, do not make you feel that when you start learning Python, contact more functions, and then give a person a kind of difficult feeling, in fact, it is not, functions often contain a lot of functions, so when writing code will be much simpler.

The len(x) function that we use the most is the len(x) function, which returns the length of the string. Note that letters, numbers, and punctuation are all part of the string. For example, len(“1234”) results in 4.

The second is STR (x), which returns the string form of x, no matter what type, so it is the opposite of eval. For example, the result of STR (1.23) is 1.23.

Python also provides base conversion functions that convert integers to hexadecimal or octal. It provides hex(x) and OCT (x) functions, which correspond to hexadecimal and octal conversions, respectively. For example, the hex(425) result is 0x1A9, and the OCT (425) result is 0O651.

There are also two less commonly used functions for Unicode encodings:

CHR (u):u is the Unicode encoding and returns the corresponding character ord(x):x is the character and returns the corresponding Unicode encodingCopy the code

Unicode encodings have not been carefully studied, know how to use the line. But I heard about the code character is quite many, the twelve constellations of small ICONS have oh.

4. String handling function methods (eight)

We have to strike while the iron is hot to be familiar with the string processing function of the eight methods, they are used in ordinary times is quite a number of, also very easy to use, have to say that Python function library really many.

Str.lower: Returns a copy of the string, printing characters in lower case. For example: print("ABCDEFG".lower()) returns ABCDEFG 2.str.upper: returns a copy of the string, printing characters in uppercase. Print ("abcdefg".upper()) returns abcdefg 3.str.split(sep=None) : returns a list of the parts of STR separated by sep. This function is a bit of a puzzle, because I didn't understand it at first, but it's easy to understand if you try the code a few times to find the differences. Such as: print (" A1B1C ". The split (" 1 ")) print (" A, B, C ". The split () ", ")Copy the code

The output is the same:

4. Str.count (sub) : return the number of times the string sub occurs in STR. 5. Str.replace (old,new) : returns a copy of the string STR, with all old strings replaced with new. Simply put, this is the substitution of strings. Replace ("n","n123.io")# replace("n","n123.io")# replace("n","n123.io")# replace("n","n123.io") The string STR is centered by width, and fillchar is optional. This is useful when writing themes in projects. Such as: "Python". Center (20, "=") results for '= = = = = = = = = = = = = = Python' 7. STR. The strip (chars) : from STR get rid of the left and right sides in its list of characters in the chars. 8. Str.join (iter) : add a STR after each element of the iter variable except the last. Join ("12345")# result is "1,2,3,4,5"Copy the code

5. END

The string type xiaobian is introduced here, if you encounter the knowledge related to it later, xiaobian will add oh. If this article has been helpful to you, you may be reviewing Python for an exam. I hope you can continue to support this article.