print statement in python

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.

  1. The usage of single quotes and double quotes is the same, but if there are the same characters in the string, use \ to escape.

For example:

  1. print('hello') 2) print ("hello") 1 and 2, the result is hello 3) print('hell'o') 4) print("hell'o") 3 and 4, the result Both are hell'o 5) print('hell”o') 6) print("hell"o”) 5 and 6, the results are both hell"o
  1. The difference between triple quotation marks and them: "What you see is what you get". It is more intuitive to use triple quotation marks for a string of html or xml format (at this time, single quotation marks or double quotation marks need to be escaped)

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

print statement in python
Functions in python
python print array
Python pass statement
How to write try statement in python
03. Operators in Python entry
Join function in Python
12. Network Programming in Python3
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Write gui in python
Python Print print timer function
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
Subscripts of tuples in Python
Talking about inheritance in Python
Noteworthy update points in Python 3.9
Containerize Python applications in 3 minutes
What is introspection in python
05. Python entry value loop statement
What is object-oriented in python
Generators and iterators in Python
Talking about strings in Python
The usage of wheel in python
Summary of logarithm method in Python
Use of Pandas in Python development
Use nohup command instructions in python
What is list comprehension in python
Is there function overloading in python
Getting started with Numpy in Python
Detailed sorting algorithm (implemented in Python)
How to wrap in python code
What does rc1 mean in python
Common errors and solutions in python
Use of numpy in Python development
What does def in python do
How to omit parentheses in Python
Detailed usage of dictionary in Python
Usage of os package in python
Learn about garbage collection in Python
How to write classes in python
How to filter numbers in python
​What are the numbers in Python?
How to read Excel in Python
There are several keywords in Python
Everything in Python is an object
How to view errors in python
What does np do in python
How to write return in python
Talking about the modules in Python
How to understand variables in Python
How to clear variables in python
The usage of tuples in python
How to use SQLite in Python
Description of in parameterization in python mysql
Understanding the meaning of rb in python
Why doesn't Python support the switch statement?
Is there an algorithm in python language