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