Reference link: Python | end parameter in print()
One, single quotes, double quotes, and triple quotes in python
Adding'r' in front of Python string is to tell the compiler that this string is a raw string, don't change your mind, and output the original string. Single quotes are used to escape, double quotes are used to enclose strings, and triple quotes are used for free line breaks. It does not matter if you use single and double quotation marks for string variable assignment, but when single quotation marks appear in the string, the single quotation mark enclosed by the single quotation mark must be escaped with a backslash, and the inner single quotation mark enclosed by the double quotation mark does not need additional processing.
For example:
Such as:
print('''
hello'
world"
''')
The result is
hello’ world”
example:
###( ') Single quotation mark In IDLE of Python, type directly: print('Hello World!')
( ") Double quotes
The same input is as follows: print("Hello World!") It can be seen that the two results are the same, single quotes and double quotes are common; but when the two are mixed together, it is easy to make mistakes, for example :
print("I said, "Don't do it"") Our original intention was to print out this format: I said, "Don't do it" but the displayed result is a prompt like this: SyntaxError: invalid syntax
Here we can use the escape character'' to avoid such errors. print("I said, "Don't do it"") By escaping the character, we can get the desired effect! !
( """) triple quotation marks
The main function of the triple quotation marks is to play the role of line break, look at the following two lines of code
Code one
print("You cannot improve your past, \nbut you can improve your future.\nOnce time is wasted, life is wasted.");
Code two
print("""You cannot improve your past,
but you can improve your future.
Once time is wasted, life is wasted.""")
The results are as follows:
print(“You cannot improve your past, \nbut you can improve your future.\nOnce time is wasted, life is wasted.”); You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted. print(“”“You cannot improve your past, … but you can improve your future. … Once time is wasted, life is wasted.”“”) You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.
Looking at the following two results, you can clearly see that the results of the two prints are the same, indicating that these texts are not processed before the end of the three quotes are entered, and you can enter a new line.
Two, print's newline output and non-wrapped output
print("\t",end="), including end=" as a parameter of print()BIF, will make this function turn off the default behavior of "automatically include newline in output". The principle is: pass an empty string to end, so that the print function will not add a newline at the end of the string, but an empty string. end=''No line break is the usage of python3.# version, 2.# version cannot be compiled, if you encounter this problem, add from future import print_function to the first line.
Three, Python string formatted output
Python supports the output of formatted strings. Although very complicated expressions may be used in this way, the most basic usage is to insert a value into a string with the string format character %s. In Python, string formatting uses the same syntax as the sprintf function in C. Examples (Python 3.0+)
#! /usr/bin/python3
print ("My name is %s and %d years old this year!"% ('Xiaoming', 10))
The output of the above example:
My name is Xiao Ming and I am 10 years old!
Recommended Posts