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
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
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
(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
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
String processing functions
Here mainly to see six main functions
(1) len (x) returns the length of the string
(2) str (x) any type of x is converted into a corresponding string
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
(negligible - on binary conversion, forgive my favorite is c ++, cout it is too easy ...)
(4)
the last two
(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
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
this figure in mind the focus of the second (split), too important, of course, other methods are also important, can not remember it.
Formatted string type
.format () method of
grooves {}
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
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
can be divided into two
before a group of three, it is easy to see examples appreciated
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:
: C representative of corresponding unicode character, the other is the hexadecimal form
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!