"Learning Python with You Hand in Hand" 6-String Identification

​In "Learning Python with You Hand in Hand" 4-Hello World! In, we executed the first line of code of the Python tour and issued our first cry in the Python world-Hello World!. Among them, we said that the'Hello World!' printed using the print function is a string.

In the next few articles, we will talk about strings in detail and some operations on strings.

Python's string function is very powerful, and many people use Python because of its powerful and flexible string manipulation functions.

In this article, we first look at how strings are identified.

As mentioned in the previous article, a string is a part enclosed by two single quotes'', or two "". In fact, there are not only single quotes and double quotes, but also three single quotes "'or three double quotes """ to identify strings.

So what is the difference between them?

There is no difference between single quotation marks and double quotation marks in terms of application effects, but they can be distinguished according to the amount of content.

If it is a few short consecutive letters, or one or two words, it can be marked with single quotes, for example:'abc','Hello World!'.

There are also the parameter values ​​in the function, because they are generally short, they can also be identified by single quotes, such as the parameters sep='', end='\n' and so on in the print function.

If it is a relatively long sentence, double quotes are recommended, for example: "Life is short, you need Python!".

Three single quotation marks or three double quotation marks are generally used when there is a lot of content and need to return lines. At this time, no matter how much content or lines are between the three single quotation marks or the three double quotation marks, it is only a character string. E.g:

'''

Why is Python worth learning?

Because

Life is short

you need Python

'''

In the example, there are three single quotation marks, and three double quotation marks are the same. You can use either one.

Triple quotation marks have an advantage. A short paragraph of content included in triple quotation marks is what you see is what you get, so that programmers don't need to think about the effects of line breaks and quotation marks.

The above demos are all in English, in fact, it is not only in English. As long as it is placed in quotation marks, it is a string. It can be English letters, numbers, Chinese, or even mixed. You can try it yourself.

In [1]: print('abc')
Out[1]: print("Hello World! is our first output.")

In [2]: print("Hello World! is our first output.")
Out[2]: Hello World! is our first output.

In [3]: print('''
        Why is Python worth learning?
        Because
        Life is short
        you need Python. ''')
Out[3]: Why is Python worth learning?
        Because
        Life is short
        you needPython.

In [4]: print('''
        Why is Python worth learning?
        因为
        1、Life is short
        2、you need Python
        ''')
Out[4]: Why is Python worth learning?
        因为
        1、Life is short
        2、you need Python

 Seeing this, will everyone have a problem, since we use quotation marks to identify strings, what should we do if our string itself is quotation marks or contains quotation marks?

At this time, you need to use the escape character-backslash \.

The escape character \ has two uses:

One is used to distinguish between grammatical symbols and ordinary symbols.

For example, the content between two single quotation marks is a string. These two single quotation marks have only grammatical meaning and will not be printed as single quotation marks.

If we want to print single quotation marks as ordinary symbols, we need to put an escape symbol \ before each single quotation mark that needs to be printed. At this time, the single quotation mark will be printed out:

In [5]: print("\'Hello World!\' is our first output.")
Out[5]: 'Hello World!' is our first output.

 In the same way, to print the backslash itself, you only need to put another backslash in front of the backslash, and it becomes \\ so that you can print a \.

The second is used to represent special symbols.

For example, the default value of the parameter end in the print function is \n, which means newline. As long as we write \n in the string, this position will return:

In [6]: print("\'Hello World!\' is \nour first output.")        
Out[6]: 'Hello World!' is
        our first output.

In Python, there are many similar escape characters that represent special symbols. The following is a list of concepts.

Escape character                

description                

\ (At the end of the line)                

Line continuation                

\\                

Backslash                

\'                

apostrophe                

\"                

Double quotes                

\a                

ASCII bell character (BEL)                

\b                

ASCII backspace (BS)                

\000                

air                

\n                

ASCII line feed (LF)                

\ v                

ASCII vertical tab (VT)                

\t                

ASCII horizontal tab (TAB)                

\r                

ASCII Carriage Return (CR)                

\f                

ASCII form feed (FF)                

\ oyy                

Octal number, the character represented by yy, for example: \o12 represents line feed                

\xyy                

Hexadecimal number, the character represented by yy, for example: \x0a represents newline                

\other                

Other characters are output in normal format                

The above is the identification method of the string. There are many more content for the calculation and operation of the string. Let's talk about it later.

 

 


Thanks for reading this article! If you have any questions, please leave a message and discuss together ^_^

Welcome to scan the QR code below, follow the "Yesu Python" public account, read other articles in the "Learning Python with You Hand in Hand" series, or click the link below to go directly.

"Learning Python with You Hand in Hand" 1-Why learn Python?

"Learning Python with you hand in hand" 2-Python installation

"Learning Python with You Hand in Hand" 3-PyCharm installation and configuration

"Learning Python with You Hand in Hand" 4-Hello World!

"Learning Python with You Hand in Hand" 5-Jupyter Notebook

For Fans: Follow the "also said Python" public account, reply to "hand 6", you can download the sample sentences used in this article for free.

Also talk about Python-a learning and sharing area for Python lovers

 

Guess you like

Origin blog.csdn.net/mnpy2019/article/details/98751873