For details about the line feed of the print function in Python 3, pythonprint

Source: Internet
Author: User

For details about the line feed of the print function in Python 3, pythonprint

Preface

Due to work needs, I recently looked at the Python application. Starting from the entry-level 9-9 multiplication table, I found that Python3.x and Python2.x are really quite different, just like the line feed processing here, i'm afraid I forget to write it down first. Well, I will not talk much about it. Let's take a look at the detailed introduction:

Code in Python2.X:

#! /Usr/bin/env python #-*-coding: UTF-8-*-_ author _ = '*****' class PrintTable (object ): '''print the 9 th multiplication table ''' def _ init _ (self): print ('start printing the 9X9 multiplication table ') self. print99 () def print99 (self): for I in xrange (1, 10): for j in xrange (1, I + 1 ): print ('% d X % d = % 2 s' % (j, I, I * j), print (' \ n ') if _ name _ = '_ main _': pt = PrintTable ()

For specific algorithms, the horizontal and vertical two-layer loops will not be mentioned.

Two problems are involved: first,print()Function end, in Pyhon2,print()To wrap a line after the output is printed, add a comma ',' to the end of the function. However, if you run the code in Python3, you will find that adding a comma does not work. Run 1 in win,


Figure 1

Run 2 in Linux,


Figure 2

We can see that there is no line feed, because in Python3, the line feed adopts a new syntax, suchprint(‘*‘, end=”)Yes, the difference is thatprint()The second parameter of the function, with end = "added, modifies, runs, and finally runs the result. 3,

Figure 3

Normal output.

In fact, there is still a problem in the above Code, connection problem 1,xrange()Function.print()Function syntax, not followedxrange()Then there will be 4 cases,

 
Figure 4

The following message indicates that 'xrange' is not defined. In Python3, The xrange () function has been integratedrange()Function, unified userange()Function.

OK. After modification, the normal result shown in Figure 3 is displayed.

Summary

Well, the above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, you can leave a message, thank you for your support.

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.