preface

We learned how to comment and how to use comments to test code. In this lesson, we learned how to use comments to test code. In this lesson, we learned how to use comments to test code. Arithmetic operators, bitwise operators, things like that, because in my experience, strings are used a lot when you’re writing code, so I’m going to take that out and talk about it, and we’ll talk about the rest of it later.

Define variables before you get started with strings

1. What are the variables? Variables are like little containers that are mainly used to hold data.

2. What is the difference between a variable and a constant? The data stored in a variable can be changed many times, as long as the program reassigns the value to the variable. What to make of the above sentence? For example: if you go to the grocery store to buy things, the price of coconut palm brand cigarettes has been 3 yuan for 10 years, then this is a constant, then the price of Lotus King, maybe 25 yuan 3 years ago, 30 yuan 1 year ago, now buy 35 yuan, then this is a variable.

Python uses the equal sign (=) as the assignment operator. For example, a = 20 is an assignment statement, which is used to load 20 into variable A ——- This process is called assignment:

4. Define constants

What is a string

A string means “a string of characters,” for example, “Hello,Virus” is a string, “How are you?” It’s also a string. Python requires that strings be enclosed in quotation marks, either single or double ——— as long as the quotation marks match. Ex. :

Escape character

When multiple single or double quotation marks (“) occur in a string, or when line breaks or Spaces are used, escape characters can be used. Case 1: Multiple single or double quotation marks example:

Solution 1:

Solution 2:

Description of escape charactersHere are a few: \n

\t

Concatenated string

Python uses the plus sign (+) as the concatenation operator for strings, as in:

If you write two strings directly next to each other, Python will concatenate them automatically, for example:

Note: Python does not allow direct concatenation of values and strings. Programs must first convert values to strings, using the repr() function.

Solution:

Raw string

Because backslashes in a string have special functions, they need to be escaped when contained in a string. For example, write a Windows path: G:\publish\codes\02\2.4. If this is not possible in python, you can use raw strings to solve this problem. The original string starts with “r” example:

A long string

Described above python multiline comment mentioned use three quotes (either single quotes or double quotes) to include the contents of a multi-line comment, it is a long string of writing, just due to the long string can be placed in any content, including single quotes, double quotes can be, if the long string is not assigned to any of the variables, The string is then ignored by the interpreter and thus commented out. If a single or double quotation mark is not assigned to a variable, it is a comment. If a single or double quotation mark is not assigned to a variable, it is a long string.

Bytes (bytes)(this can be understood, I only use in the development of some images)

Python3 added the bytes type to stand for strings (a word coined by the author to correspond to strings). A string (STR) consists of multiple characters and operates in the unit of characters. Bytes consist of multiple bytes and are operated on in units of bytes.

Bytes and STR support essentially the same methods except for the data unit in which they operate, and bytes is also an immutable sequence.

The Bytes object is only responsible for recording data in a sequence of bytes (binary format), and it is up to the program to decide what the data represents. If the appropriate character set is used, a string can be converted to a byte string. Conversely, the byte string can be restored to its corresponding string.

Because bytes hold raw byte (binary format) data, bytes objects can be used to transfer data over the network and to store files in a variety of binary formats other than pictures, music, and so on.

If you want to convert a string into a bytes object, there are three ways: 2. Call the bytes() function (which is a constructor of bytes) to convert the string to bytes in the specified character set. If no character set is specified, utF-8 character set is used by default. 3. Call encode() of the string itself to convert the string to bytes according to the specified character set. If no character set is specified, utF-8 character set is used by default.

Ex. :

Extracurricular knowledge supplement

Here is a brief introduction to the concept of character sets. The bottom layer of the computer does not save characters, but programs always need to save various characters, so what to do? Computer “scientists” came up with a way to number each character. When the program wants to save a character, it actually saves the number of that character. When the program reads characters, it actually reads numbers, and then it has to look up the “number – character mapping table” (referred to as the code table) to get the actual characters. So-called character set, therefore, is the sum of the Numbers of all characters, early americans give English characters, Numbers and punctuation characters such as the number, they think all added up to no more than 100 characters, as long as 1 byte (8 bits, support 256 characters number) can be all characters number — — — — — – this is the ASCII character set. Later, Asian countries began numbering their own characters ———–, creating their own character sets, which were not compatible. So the Americans again unified numbering of all the characters in the world’s written languages, this time using two bytes (16 bits, 65536 character numbers supported), the Unicode character set. The actual use of UTF-8, UTF-16, and so on are in the Unicode character set.

For example, I can number the same character “love” as 99, you can number it as 199, so the same number in different character sets may represent different characters. Therefore, if you use different character sets to generate bytes objects for the same string, you will get different bytes objects.

conclusion

Today the content of class is a little bit more, I hope you can carefully read, and slowly in the editor on practice, practice, don’t just see that is no help for your study, only by practicing a lot more, basic ability strong, can load on the, well, that’s all for today the content of lesson, if in the process of learning, have any problems, Scan the following pictures to pay attention, contact the author, you can help you solve the next class will be a string in-depth study, please look forward to oh!