LearnPythonTheHardWay Ex1~10

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

转义字符
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值