Python Beginner Series - string

String

Representation

There are two types string representation total of 4
(1) represented by a single or double quotes, shows only a single row string
(2) is represented by three or three single quotes one pair of double quotation marks, the string may represent a multi-line
injection : here representation of three single quotes in python is also a multi-line comments, depending on whether the string contents of three single quotes is assigned to a variable
Note: If you want to quote strings can represent how to do? This single or double quotes may be used in an alternating manner, i.e., if desired text appears in single quotes, double quotes for use, and vice versa.

Character string number

python provides two ways to index a string, but it is quite easy to use, this is it compared to c / c ++ is an advantage
Here Insert Picture Description

Use string

There are two operating string, indexing and slicing frequently used, they are used to obtain a [] or more characters
index: string variable Name [M]
slices: string variable Name [M: N] (expressed in M position at the beginning, until not including the end of the N-th position)

Advanced Usage string sections
(in fact, there is no more advanced Rights)
(1) M and N are missing can be expressed to the beginning / end
(2) step
string variable name [M: N: K] shows a step K the string sections
Here Insert Picture Description
Note: If there are children's shoes to understand the syntax of MATLAB, you will find them very similar in this regard, pyhon matlab for simplicity and string indexing and slicing process really great.
(3) in reverse output
[:: --1] It will be appreciated in steps of -1
Here Insert Picture Description

(4) escape character
escapes express intention particular character, and this c ++, java is the same, the characters \ after the character is meant to be as understanding.
\ b rollback
\ n newline
\ r carriage return

String simple operation

(1) x + y connection string
(2) n * x n times to copy the string
(3) x in s, it is determined whether the substring, a return to true, ...

A very simple example
Here Insert Picture Description
Here Insert Picture Description
of this very simple little program can be blind to write about, write about and more still very conducive to exercise their base, slowly good foundation, in some programming contests such as acm, csp in their programming skills and speed will not consciously have greatly improved.

Try to reduce the string weekStr, the connection operation

Here Insert Picture Description

String processing functions

Here mainly to see six main functions
(1) len (x) returns the length of the string
Here Insert Picture Description
(2) str (x) any type of x is converted into a corresponding string
Here Insert Picture Description
Note: the str corresponding to the same time we must remember live eval function, which is to remove the marks, python program executable statement, the former is increased marks
(3) hex (x) / oct (x) conversion of a different band
Here Insert Picture Description
(negligible - on binary conversion, forgive my favorite is c ++, cout it is too easy ...)
(4)
the last two
Here Insert Picture Description
(negligible - this personal feeling do not mind, general programming, this is still relatively little used)

Note: python unicode encoded form in all, this coding is committed to covering all of the characters in the world, will each code corresponding to a character, which also makes Python programs can circulate all over the world.
Some interesting examples
Here Insert Picture Description
following this very magical, unicode encoded in the output actually 12 constellations Oh!

String Processing Method:

Methods: The function can be understood as, possibly java classes and objects better explanation of the method
(if you want to learn java or c ++, it must be repeated understand a word: class is an abstract object, the object is an instance of the class), this idea will run through the whole program.
Figure it directly put
Here Insert Picture Description
this figure in mind the focus of the second (split), too important, of course, other methods are also important, can not remember it.
Here Insert Picture Description
Here Insert Picture Description

Formatted string type

.format () method of
grooves {}
Here Insert Picture Description
will be added to the parameters after the method of the position corresponding to the groove in a corresponding order
, of course, be set manually filling sequence
Here Insert Picture Description
Python output formatting and c are most similar, I think this is because the overall Python simplicity determines that it must be defined on the output format, because there is no defined input memory.
Tank may be further control of the parameters
: the boot parameters in the output format of a slot position control
Here Insert Picture Description
can be divided into two
before a group of three, it is easy to see examples appreciated
Here Insert Picture Description
the format means output width is set to 20 , vacant space is filled with the sign bit =, ^ is centered, aligned substantially three are used together
^ into> is right-aligned, the default is left-aligned
three examples after:
Here Insert Picture Description
Here Insert Picture Description
: C representative of corresponding unicode character, the other is the hexadecimal form
Here Insert Picture Description
of these believe that a look at the results on a good understanding of it!
Today to introduce it here, I have time to add another modification, have any questions feel free to correct me Ha!

Published 37 original articles · won praise 1511 · Views 160,000 +

Guess you like

Origin blog.csdn.net/weixin_43943977/article/details/102092709