DAYS1 Summary of basic learning in Python

Source: Internet
Author: User

First, the use of escape characters

In practical applications, we may want to refer to someone else's words or use abbreviations, this time, in the print () function, in order to print the quotation marks as characters, you have to use the escape character "\".

For example, when we print the He says, "Forget the memories,continue to is Life,miss,just pass by." , you can do this

>>>print ("He says, \" Forget the memories,continue to be Life,miss,just pass by.\ ")
#反斜杠 \ In front of quotation marks, double quotation marks as one character

In addition, you can do this

>>>print ('He says, ' Forget the memories,continue to being Life,miss,just pass by. ' ) ')
#当要打印的语句里面有引号时, you can use a different quotation mark as an indicator, which can do the same thing.
>>>print ('Wise men learn by other men\ 's mistakes,fools by their own') #通过在men ' s ' in front plus backslashes \, Makes Python think of a character so that it can perfectly solve the wise men learnby the other men'smistakes,fools by their own

Two, concatenate multiple strings

Concatenate multiple strings Yes, use the "+" sign. However, if you want to output a result with an interval, you need to increase the space string

>>>print ('Alex''ammy') Alex Ammy

Of course, if you use the print () function, you can separate the strings by using commas directly:

>>>print ('Alex','ammy','Mike ' ) Alex Ammy Mike

In addition, if we want to customize the output format, you can use the format specifier "%", and you can add a value that represents the length after "%", for example

>>>print ("%s%s%10s" % ('John','Mike', ' Mans ' )) John Mike        man
#10表示一个长度为10的字符串, the third character man takes 3 bits, then it fills 7 spaces

>>>print ("%f"% (43.5-36.1))
7.400000

>>>print ("%.2f"% (43.5-36.1))

7.40


If you want to print "%", you can add a "%" before "%".

DAYS1 Summary of basic learning in Python

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.