python--Script and Print

Source: Internet
Author: User

Scripts and Print

1. script files

The P118 page in the basic Python Tutorial (second edition), the original operation is the following:

1 _metaclass_ = typeclass person :4     def  SetName (self,name):5         Self.name = name6     def  getName (self):7         return  self.name 8     def  greetname (self):9          print"hello.world I ' m%s "%self.name

Then, at the command line, enter

>>foo = = Person ()

>>foo.setname (' Luke Skywalke ')

>>foo.greet ()

Complete script after modification to text:

1 #!/usr/bin/env python2 #Coding=utf8 #汉字需要加上这个 to prevent garbled characters3 4_metaclass_ =type5 6 classPerson :7     defSetName (self,name):8Self.name =name9     defGetName (self):Ten         returnSelf.name One     defGreetname (self): A       #print "Hello.world I ' m%s"%self.name -         Print "hello.world,i am", Self.name#these kinds of output formats can be -       #print Self.name + ' Hello ' the       #print self.name, ' Hello ' -Foo =Person () -Foo.setname ('Luke Skywalker') -Foo.greetname ()

Operation Result:

  

Note 1: function output and invocation , in the end do not need to be written in print foo.greetname (), directly written as Foo.greetname (), should be called the function foo.greetname (), the function comes with print, If there is no call, the output will need to be written in print output form.

2.Print Output form : Divided into formatted output (with%) and direct output (without%) See Web page: bbs.csdn.net/topics/390277547?page=1

Format output% and then add the appropriate type

The direct output is connected by ' + ' or ', '

        What you see in print
Print ("%s is%d" %(name,age))
"%s is%d" and (name,age) the middle of the percent, which represents the delimiter for formatting the string .
% preceding "%s is%d" indicates what to output, with variable type
The trailing (name,age) represents the variable that corresponds to the%xxx in the preceding output      
        if not with% , that is:
        Print ("%s is%d old"% (name,age))
can also be written
        Print (name, ' is ', str (age), ' old ')
Or is:
        Print (name + ' is ' + str (age) + ' old ')
where STR (age) means to convert the integer 25 to the string ' 25 '

 

      

python--Script and Print

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.