The text code of the print function wrapping in Python3

Source: Internet
Author: User
Recently in the study of Python3, found a problem to summarize, so the following this article mainly introduces you to the Python 3 in the print function to wrap the relevant data, the text through the sample code introduced in very detailed, to the needs of friends have a certain reference learning value, Friends interested in the following with the small series to learn together.

Objective

Because of the needs of the work, recently looked at the application of Python, starting from the entry-level 99 multiplication table, the results found that python3.x and python2.x really is a lot of difference, such as the line here to deal with, afraid to forget to write down first, OK, words not to say, come together to see the detailed introduction:

Code in python2.x:


#!/usr/bin/env python#-*-Coding:utf-8-*-__author__ = ' * * * ' class printtable (object): ' Print 99 multiplication table ' Def __init__ (self) :  print (' Start printing a multiplication table of 9 X 9 ')  self.print99 () def print99 (self):  for I in xrange (1, ten): for   J in xrange (1, I +1):    print ('%d X%d =%2s '% (j, I, I*j)),   print (' \ n ') if __name__ = = ' __main__ ': pt = printtable ()

The specific algorithm, horizontal, vertical two-layer cycle will not say.

Here are two questions: first, at the end of the function of the inner loop, in print() Pyhon2, print() after printing the output to wrap the word, it is at the end of the function with a comma ', ', but in the Python3 run the code will find that the comma does not work, in win Run 1 is shown,


Figure 1

Running 2 in Linux,


Figure 2

As you can see, there is no line break, because in Python3, the new line is used in the syntax, shape print(‘*‘, end=”) , yes, the difference is in the print() function of the second parameter, plus the end= ", modify, run, the last run result, 3,

Figure 3

Normal output.

In fact, there is a problem in the above code, then the above question one, xrange() function, if you just modified the print() function of the syntax, no attention xrange() , there will be 4 of the situation,


Figure 4

It is suggested here that ' xrange ' is not defined, in fact, is in Python3, the xrange () function has been integrated into range() the function, unified use of range() functions.

OK, after the modification, it will output the normal result of Figure 3.

summary

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.