Exercise 3
· / slash
· * asterisk
Exercise4
Strings are really handy, so in this exercise you will learn how to make strings that have variables embedded in them. You embed variables inside a string by using a special {} sequence and then put the variable you want inside the {} characters. You also must start the string with the letter f for “format”, as in f"Hello {somevar}". This little f before the " (double-quote) and the {} characters tell Python 3, “Hey, this string needs to be formatted. Put these variables in there.”
Exercise 5
round(1.7333) = 2
print(f"Let's talk about {my_name}.")
#python2.0使用的方式是 print("Let's talk about %s" % my_name)
print(f"She's {my_height}cm tall.") #python2.0使用的方式是 print("She's %d cm tall" % my_height)
Exercise 6
hilarious = False
joke_evaluation = "Is't that joke so funny?! {}" #和Exercise5不同的另一种方式
print(joke_evaluation.format(hilarious))
Exercise 7:More Printing
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
value: 表示要打印的值 , 各个值之间用‘,’(逗号隔开)
file: a file-like object (stream); defaults to the current sys.stdout.
# 把python is good保存到 a.txt 文件中
f = open(r'a.txt', 'w')
print('python is good', file=f)
f.close()
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
f = open(r'a.txt', 'w')
print('python is good', file=f, flush=True)
# 正常情况下print到f中的内容先从到内存中,当文件对象关闭时才把内容输出到 a.txt 中,当flush=True时它会立即把内容刷新存到 a.txt 中
>>> print(1, 2)
1 2
>>> print(1, 2,end='1')
1 21>>> print(1, 2, sep='!')
1!2
>>>
Exercise 10 Escape sequences
转义字符