Home  >  Article  >  Backend Development  >  A closer look at Python variables and strings

A closer look at Python variables and strings

高洛峰
高洛峰Original
2017-03-24 16:46:321494browse

LearnPythonThe same is true. The most convenient way to start learning a new language is to imitate, and then innovate through imitation. In the initial imitation process, it is necessary to type out every line of code and type out every punctuation mark in person, rather than simply reading ten lines at a glance without actual operation. In this way, even if you read the entire book, you may still not be able to write a program.

This is the second article about Python, mainly introducing variables and strings.

(1)

Variables:

Simply put, variables are the most basic storage units in programming, and they can store what you want to put in them. For example, assign a value to variable "a":

A closer look at Python variables and strings

## Operation steps: Open the Mac terminal - enter "python3" and press Enter , enter the Python3.6 environment - enter "a=25" and press Enter. At this time, the assignment to a has been completed; enter a again, and after pressing Enter, you can see the assignment result to a.

A closer look at Python variables and strings

Note: Python is case-sensitive, a and A are two different variables, please pay attention when writing.

print():

print() is a common function in Python, As the name suggests, it prints the content in the brackets. It can be understood simply this way. For example, assign a value of 25 to variable a, and then print a out. Enter the following content in PyCharm:

A closer look at Python variables and strings

If you forget to assign a value to variable a, PyCharm will report an error during runtime. You need to report the error accordingly. Modify the prompt information accordingly.

The name "a" here is undefined, and Python cannot print objects that do not exist.

(2)

String:

Simply put, a string can express the content in single quotes, double quotes, or triple quotes. Here, single quotes and double quotes are the same.

  • #'Content in single quotes'

  • "Content in double quotes"

  • '''Triple quotes are usually used for longer content , you can wrap the line at will'''

A closer look at Python variables and strings

##Next try to enter this paragraph in PyCharm Code:

A closer look at Python variables and strings

## Display after running:

A closer look at Python variables and strings

For my_intro, I added the first two variables, and then printed out my_intro, or you can directly print (what_do_i_do + what_i_like).

Here we talk about addition, of course, multiplication can also be done.

A closer look at Python variables and strings##After multiplying, you get:

A closer look at Python variables and stringsIf you want to

comment

out part of the code, directly select the part, and then use the shortcut key "command+/" to implement batch comments. Finally look at this code:

A closer look at Python variables and stringsGet this result :

At this time, an error message appeared, indicating that it must be of str type, not int type. The reason is that string (string) is just a data type in Python, and the other data type is the integer type(integer ), two different data types cannot be added, and corresponding conversion is required.

If you don’t know what type the variable is, you can enter print(type(Variable name)) in the compilation box to check the variable type.

So, for the example of the error reported above, the correct way is to convert the string type to int, add the two, and finally get the result 1834 .

A closer look at Python variables and strings

Finally, try to solve a slightly more complicated problem:

A closer look at Python variables and strings

In Python, the len() method returns the length of the string. The length of string1 here is 22. Subtracting num, you will eventually get 2 Hellos! .

At this point, you have basically mastered the basic usage of variables and strings. I will introduce the sharding and indexing of strings later.

It is strongly recommended that you practice it yourself and type out these codes line by line. You may find some problems that cannot be detected visually. You can also draw inferences from one example during practice to experience the results after successful operation. A small sense of joy and accomplishment.

The above is the detailed content of A closer look at Python variables and strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn