1. Expresses:
Strings can be enclosed in single or double quotes, and the effect is exactly the same.
For longer strings, you can also enclose them in three quotation marks, "" " ... " "" "or" "... ". Quotation marks are then used with different types of quotation marks to be treated as ordinary characters, without error, as
' Yes, ' he said. ', or "doesn ' t".
2. Line break:
strings can span multiple lines. One way is to use the three quotes: "" "..." "" or "...". line end newline characters are automatically included in the string, but you can avoid this behavior by adding \ At the end of the row.
3. Output:
In the interactive interpreter, the output string is enclosed in quotation marks, and special characters are escaped with backslashes.
The print () function produces a more readable output that omits quotes and prints out special characters that are escaped.
4. Escape:
If you want to output a string that contains a single quotation mark ('), how does it represent the string? For example, what is the string "What's your name?" You can't use ' what ' your name? ' to indicate that Python doesn't know the word
The starting and ending positions of the string. Therefore, the single quotation mark in the middle of the string should be specified to not represent the end of the string. This can be achieved with the help of the call escape character \. That is, the string can be represented as ' what\ ' your name? '.
Another way is to use double quotes "What ' s your name?".
Similarly, double quotation marks must be used in double quotation marks in a string.
Finally, you must use the escape character \ \ to represent the backslash.
What if you want to specify two lines of string? One way is to use the previously mentioned string with three quotes, or you can use the escape character \ n to indicate the beginning of a new line. For example, the. RST Line\nthis is
The second line. Another useful escape character is the Tab key--\ T.
It is necessary to note that in a string, a backslash at the end of a line simply represents the string of the next line
Is the continuation of the previous line, but does not add new rows.
5. Natural string:
If you want to specify something that is not handled specifically, such as an escape sequence, you need to specify a natural string by appending R or R to the string.
For example, R "Newlines is indicated by \ n".
note in particular that the following two natural strings will give an error: R ' C:\appdata\ ' (the original string cannot have a backslash at the end, which obviously makes the natural string look less graceful), and
R ' C:app ' data ' will also error, but R ' c:app\ ' data ' will not error.
(Personal understanding of the above questions: first you have to conform to the normal expression of the string, you can let the compiler parse out is a string, and then ' get ' into a natural string).
6. Operation:
Strings can be joined with the + operator, or they can be repeated multiple times with the * operator.
Two or more adjacent string literals (enclosed in quotation marks) are automatically connected. However, this method can only be used for the connection of two strings, the variable or the expression is not possible.
If you want to concatenate multiple variables or concatenate a variable and a constant, use the + function, which is especially useful when you want to slice a long string.
7. Index:
A string can be indexed, and the first character has an index value of 0. Python does not have a separate character type; a character is a string of length 1.
The index can also be a negative value, at which point it counts from the right. such as Word[-1] #last character.
8. Slices:
In addition to indexes, slices are supported. indexes are used to get a single character, and slices let you get substrings. such as Word[0:2] #characters from position 0 (included) to 2 (excluded).
Note that the character that contains the start, does not contain the character at the end. This makes s[: i] + s[i;] Always equals S.
the index of the slice has a very useful default value; The first index omitted defaults to zero, and the second index omitted defaults to the size of the sliced string.
Attempting to use an index that is too large results in an error. However, when used for slicing, it is very elegant to handle. (Give yourself a try).
9. Immutability of Strings:
PYthon strings cannot be changed-they are immutable . Therefore, assigning a location to a string index results in an error.
If you need a different string, you should create a new one.
Note: The built-in function Len () returns the length of the string.
2015-12-30 10:49:43
Strings in Python and their related operations