Deep understanding of Python variables and strings

Source: Internet
Author: User
Learning Python is the same. The most convenient way to start learning a new language is to imitate and then innovate in imitation. In the early imitation of the process, it is important to do to personally knock down each line of code, play every punctuation, rather than simple yimushihang, not practical operation, so even after reading the whole book, may still not write the program.

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

A

Variable:

In simple terms, variables are the most basic unit of storage in programming and can store what you want to put in. For example, assign a value to the variable "a":

Operation steps: Open the Mac terminal-enter "Python3" after entering, enter the Python3.6 environment-enter "A=25" after the return, at this time has been completed to a assignment, and then enter a, you can see the assignment result of a.

Note: Python is case sensitive, a and a are two different variables, and you need to be careful when writing.

Print ():

Print () is a common feature in Python, as the name implies, to print the parentheses, so that you can understand it simply. For example, assign a value of 25 to variable A, and then print a. Enter the following in the Pycharm:

If you forget to assign a value to variable a, the run-time Pycharm will give an error and need to be modified according to the error message.

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

Two

String:

Simply put, a string can represent the contents of single quotes, double quotes, or triple quotes, where single and double quotation marks are the same.

    • ' Content in single quotes '

    • "Content in double quotes"

    • "' Three quotation marks are usually used for longer content, and can be changed at will."

Next try entering this code in Pycharm:

Display after running:

For My_intro, I add the first two variables, print out the My_intro, or print directly (What_do_i_do + what_i_like).

This is said to add, of course, can also be multiplied.

After multiplying, you will get:

If you want to comment out some of the code, select the section directly, and then shortcut "command+/" to implement bulk annotations.

Finally, let's look at this piece of code:

Get this result:

At this point the error message appears, the hint must be str type, cannot be int type. The reason is that the string is just one of the data types in Python, and the other is an integer type, and the two different data types are not additive and need to be converted accordingly.

If you do not know what the variable is, you can enter print (type (variable name)) in the Compile box to see the variable type.

So, for this example of the above error, the correct way to convert the string type to int, the two are added, and finally the result is 1834.

Finally, try to solve a slightly more complex problem:

In Python, the Len () method returns the length of the string, where the length of the string1 is 22, minus Num, resulting in 2 hello!.

Here, basically you've mastered the basic usage of variables and strings, and later I'll introduce the shards and indexes of the strings.

It is strongly recommended that you go to practice in person, one line to knock out the code, you may find some problems can not be seen, but also in the process of extrapolate, experience the success of the small joy and sense of accomplishment.

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.