9.11 前9习题

1.print 

(1)打印的第一句 。。。。

1 print " Hello World! "

注意""和''的使用

1 print "i'm paul"   #
2 
3 print 'i'm paul'  # 错   改为 'i \'m paul'

以及""" """和''' '''的使用

1 print  """
2 
3 Hello World!
4 
5 Hello World!
6 
7 Hello World!
8 
9 """

(2)逗号

print "hello world ",'abc'

print "i am paul "

结果:

hello world abc

i am paul

print "hello world "  'abc'

print "i am paul "

结果:

hello world abc

i am paul

print "hello world" ' abc',
print "i am paul"

结果:

hello world abci am paul

print "hello world" ' abc',
"i am paul"

语法错误

(3)运算符号

print "now i will count the eggs:"
print 3+1+2-5+4%2-1/4
print 3+2>4-2

结果:

now i will count the eggs:

1

True

(4)变量及打印

name = "paul"
age = 20
height = 70

print "let's talk about %s"%name
print "he's %d years old"%age
print "he's %d inches tall"%height


print "let's talk about %r"%name
print "he's %r years old"%age
print "he's %r inches tall"%height

print "if i add %d and %d,i get %d."%(age,height,age+height)

let's talk about paul

he's 20 years old

he's 70 inches tall

let's talk about "paul"

he's 20 years old

he's 70inches tall

if i add 20 and 70,i get 90

x = "there are %d types of people."%10
print x 

print "i said :%r "%x

y =  "isn't that joke so funny? %r"
z = False

print y%z

a = "this is the left of"
b = "a string with a right side"

print a + b

结果:
there are 10 types of people.

i said :there are 10 types of people.

isn't that joke so funny?False

this is the left of a string with a right side

print "ab " *10

结果:
ab ab ab ab ab ab ab ab ab ab

formatter = "%r %r %r"

print formatter %(1,2,3)
print formatter %("one","two","three")
print formatter %(True,False,True)
print formatter %(
   "i had this string",
   "that you could ",
   "type  up right"     
  
)

1 2 3
'one' 'two' 'three'

True False True

'i had this string' 'that you could' ' type up right'

1 a = "a\nb\nc\n"
2 print a 

结果:

a

b

c

原文地址:https://www.cnblogs.com/fanyear/p/4802297.html