书写格式

来源:互联网 发布:淘宝中国一号白银店铺 编辑:程序博客网 时间:2024/04/27 21:41
Can we write x=100 instead of x = 100?
You can, but it's bad form. You should add space around operators like this so that it's easier to read. 

 

 

 

 

How can I print without spaces between words in print?
You do it like this: print "Hey %s there." % "you". You will do more of this soon.

you will get Hey you there·····························

非常重要

 

这里的print函数 “”当中出现的%d %s等正如c当中的一样,后面紧跟自己要添加的数据

 

不同之处

print "If I add %d,%d, and%d I get%d."%(my_age,my_height,my_weight,my_age+my_height+my_weight)

 

这里并不是分开写,而是用()来集中起来

 

 

变换数据类型时,要注意

eg

int(raw_input(“enter your name:”))

也就是注意input 和 raw_input之间的区别

input 假定用户输入的都是合法的Python表达式

raw_input 把所有的输入当做原始数据,然后将其放入字符串中

 

比如

name=input("What's your name?")

print "Hello,"+name+"!"

只有在输入为"canlan"时才能行

 

 

name=raw_input("What's your name?")

print "Hello,"+name+"!"

只有在输入为 canlan 时就可以了

 

Strings may contain the format characters you have discovered so far. You simply put the formatted variables in the string, and then a% (percent) character, followed by the variable. Theonly catch is that if you want multiple formats in your string to print multiple variables, you need to put them inside( ) (parenthesis) separated by, (commas).

 

 

strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))

print strHello

#输出果:the length of (Hello World) is 11

 

这里不管使用‘hello world’还是 “hello world”   都返回一样的结果

 

 

 

x = "There are %d types of people." % 10

binary= "binary"

do_not= "don't"

y= "Those who know%s and those who%s."%(binary,do_not)

printx

printy

print"I said: %r."%x

print"I also said: '%s'."%y

hilarious= False

joke_evaluation= "Isn't that joke so funny?! %r"

printjoke_evaluation % hilarious

w= "This is the left side of..."

e= "a string with a right side."

print w +e##注意这里的加号···表示两个字符串的相连···

这里print "I also said: %s" %y 时输出 I also said:Those who ...

这里print "I also said:' %s'" %y 时输出 I also said:'Those who ..'.

 

 

 

What is the difference between %r and %s?

 

Use the %r for debugging, since it displays the "raw" data of the variable, but the others are used for displaying to users.

 

What's the point of %s and %d when you can just use%r?

 

The %r is best for debugging, and the other formats are for actually displaying variables to users.

 

If you thought the joke was funny could you write hilarious = True?

 

Yes, and you'll learn more about these boolean values in exercise 27.

 

Why do you put ' (single-quotes) around some strings and not others?

 

Mostly it's because of style, but I'll use a single-quote inside a string that has double-quotes. Look at line 10 to see how I'm doing that.

原创粉丝点击