Home > Backend Development > Python Tutorial > Detailed explanation of graphic code for line wrapping of print function in Python3

Detailed explanation of graphic code for line wrapping of print function in Python3

黄舟
Release: 2017-08-08 10:58:34
Original
1645 people have browsed it

I was learning python3 recently and found a problem that I wanted to summarize. So the following article mainly introduces you to the relevant information about the line wrapping of the print function in Python 3. The article introduces it in detail through the example code and provides the necessary information. Friends, it has certain reference and learning value. Friends who are interested, please follow the editor to learn together.

Preface

Due to work needs, I recently looked at Python applications, starting with the entry-level multiplication table, and found that There are really big differences between Python3.

#Code in Python2. . There are two issues involved here: First, at the end of the

print()

function in the inner loop, in Pyhon2,
print()

requires a newline after printing. If so, add a comma ',' at the end of the function, but when you run the code in Python3, you will find that adding a comma does not work. When running in win, as shown in Figure 1,

Figure 1
Running in Linux is shown in Figure 2,



Figure 2

It can be seen that, There is no line break. The reason is that in Python3, line break adopts new syntax, which is in the form of
print('*', end=”)

. Yes, the difference is in

print() The second parameter of the function is added with end=", modified and run. The final running result is as shown in Figure 3,

Figure 3Normal output.

In fact, there is another problem in the above code, which is continued from the above question 1,

xrange() function, if you just modified the

print()

function syntax, if you don’t pay attention to
xrange()

, the situation in Figure 4 below will appear,

##Figure 4 The prompt here says 'xrange' is not defined. In fact, in Python3, the xrange() function has been integrated into the
range()

function, and the

range() function is used uniformly.

OK, after modification, the normal result in Figure 3 will be output.

Summarize

The above is the detailed content of Detailed explanation of graphic code for line wrapping of print function in Python3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template