Python: Recognition of variables and strings, python variable strings

Source: Internet
Author: User

Python: Recognition of variables and strings, python variable strings

A few months ago, I began to learn about personal image management, from hair styles, makeup, costumes to instruments and manners. I started to make new changes to my personal style, the most basic thing is to first understand which style you belong to, and then find the reference object to imitate. It can be a person, star, or model you like, until innovation ends, form your own unique personal style.

The same is true for learning Python. The most convenient way to start learning a new language is to imitate and then innovate in imitation. In the initial imitation process, it is necessary to knock on each line of code in person and print every punctuation, rather than simply writing ten lines, without actual operations, even if you read the complete book, maybe the program still cannot be written.

This is the 2nd article on Python, which mainly describes variables and strings.

(1)

Variable:

To put it simply, variables are the most basic storage unit in programming and can store what you want to put in. For example, assign a value to the variable ":

Operation Procedure: Open the Mac terminal -- Enter "python3" and press enter to enter the Python3.6 environment -- Enter "a = 25" and press Enter. At this time, the assignment to a has been completed, and then enter, after you press enter, you can see the result of assigning values to.

Note: Python is case-sensitive. a and A are two different variables. Note this when writing.

Print ():

Print () is a common function in Python. As the name suggests, print the content in the brackets. You can simply understand it. For example, assign a value to variable a to 25 and print. Enter the following content in PyCharm:

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

The name "a" is not defined here. Python cannot print non-existent objects.

(2)

String:

To put it simply, a string can express the content in single quotes, double quotes, or three quotes. Here, single quotes are the same as double quotes.

  • 'Content in single quota'
  • "Content in double quotation marks"
  • '''Three quotation marks are usually used for long content and can be wrapped at Will '''

Next, try to enter this code in PyCharm:

After running:

For my_intro, I add the first two variables and print my_intro. You can also directly print (what_do_ I _do + what_ I _like ).

Here we talk about the addition. Of course, we can also multiply them.

After multiplication, you will get:

If you want to comment out a part of the code, directly select the part, and then press the shortcut key "command +/" to implement batch comments.

Finally, let's look at this Code:

The result is as follows:

In this case, an error message is displayed. The prompt must be 'str' type, not 'int' type. The reason is that string is only one data type in Python, And the other data type is integer. The two data types cannot be added, the conversion is required.

If you do not know the type of the variable, you can enter print (type (variable name) in the compilation box to view the variable type.

In this example, the string type can be converted to int type in the correct way. The result 1834 is obtained after the two values are added.

Finally, try to solve a slightly complex problem:

In Python, the len () method returns the length of the string. Here, the length of string1 is 22, minus num, and two Hello! will be obtained !.

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

It is strongly recommended that you practice the code in person and work out the code one by one line. You may find some problems that cannot be visually tested. You can also put them apart in the practice process, experience the little joy and accomplishment after successful operation.

Operating Environment: Python version, 3.6; PyCharm version, 2016.2; Computer: Mac

----- End -----

Author: du wangdan, Public Account: du wangdan, Internet product manager.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.