python basis tris (String)

A string

1.1 What is the string

字符串是python中不可变的一种数据类型,主要用来表示文本。

1.2 it Is creating a string

the right side is a variable name is double quotes character, as long as it is enclosed in quotation marks are considered to be of python string.
python basis tris (String)

Here is used in single quotes, double quotes exactly the same result on the map, then why are single and double quotes it? Because in some cases it may be very applicable.
python basis tris (String)

Below, the first string comprising an apostrophe (herein called prime feel more appropriate), and therefore can not use the string enclosed in single quotes, or error. Here we use double quotation marks.
Note: In the figure above string is 'Let', python and therefore do not know how to deal with the rest of the content, because the apostrophe after the python's t as the terminator, so we need to make use double quotes to distinguish python, the same If the double-quoted string needs then out to single quotes
python basis tris (String)

Here you can also use \ to escape quotation marks, apostrophes just tell python, rather than endings.
ps: quotes like this to escape useful under certain circumstances must be such as to a string containing single quotes and double quotes, such as (Let's say "hello") , of course, if you do not want to use a lot of \ have the means solve, you can use a long string as well as the specific content of the original string will talk later.
python basis tris (String)

1.3 string concatenation

Like them, like adding digital
python basis tris (String)

1.4 long string

To represent very long string (string across multiple lines), three double quotes may be used (instead of the normal marks), three single quotes may be used.
Please note: three single or double quotes allow python to identify the start and end positions it can not contain single and double quotes, without having to use \ to escape.
python basis tris (String)


Tip: conventional string can span multiple lines, as long as the end of the line with a backslash. For example, if you write the following code:

print("hello \
world")

Will print hello world


1.5 original string

Original string is not in a special manner backslash, so it is suitable in some cases.
In conventional string backslash has a special feature that allows you to include characters not originally included in the string, this is usually very good, but there may be a problem in some cases. E.g:
python basis tris (String)

I want here is c: \ windows \ new, print out the obvious but not what I want, of course, you can then use a backslash to escape prior to the slash, but how to do a long path ? In such a case the original string came in handy, because they do not do special treatment backslash, but let strings are intact

python basis tris (String)

The original string is represented by the prefix r (figure), seems to find that the original string can contain any character, amount. . Probably right, with one exception, quote needs to be escaped so as usual, but also an escape for performing backslash will be included in the final string (below), Furthermore, the original character string symbol can not single backslash at the end, unless you escape it, but the backslash and escape the following figure for the same will be displayed, if not escape, python will not be able to determine whether a string ends.
python basis tris (String)

A legacy issue, it says that a string can not end with a backslash, but have to use the words may be configured such, this approach can be considered a mosaic, only when you enter two strings, this mechanism only works
python basis tris (String)

这里只说下字符串,具体的字符串方法将会在之后说明

Guess you like

Origin blog.51cto.com/12020040/2426763