Days1 summary of python basic learning

First, the use of escape characters

In practical applications, we may need to quote other people's words or use English abbreviations. In this case, in the print() function, if you want to print the quotation marks as characters, you have to use the escape character " \ ".

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

>>>print ( " He says, \"Forget the memories,continue to be life,miss,just pass by.\" " ) 
#The backslash \ is in front of the quotation mark, and the double quotation mark is treated as a character

Alternatively, you can do

>>>print ( ' He says, "Forget the memories, continue to be life, miss, just pass by." ' ) 
#When there are quotation marks in the statement to be printed, different quotation marks can be used as indicators , which has the same effect
>>>print('Wise men learn by other men\'s mistakes,fools by their own')
#By adding a backslash \ before the ' in men's, python is made to think it is a character, so that it can be solved perfectly
Wise men learn by other men's mistakes,fools by their own

Second, concatenate multiple strings

To concatenate multiple strings, use the "+" sign. But if you want to output results with gaps, you need to add space strings

>>>print('alex' + ' ' + 'ammy')
alex ammy

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

>>>print('alex','ammy','mike')
alex ammy mike

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

>>>print("%s %s %10s" % ('john','mike','man') )
john mike man 
#10 represents a string of length 10, the third character man occupies 3 bits, then it will be filled with 7 spaces

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

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

7.40


If you want to print "%", add another "%" before "%".

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325094241&siteId=291194637