零py-(ep-02)
1、原始字符串:在要输出的字符串前面加“r”,所有转义字符将失去作用,e.g print(r"\how \are \you \doing?"),结果将输出原字符串:\how \are \you \doing2、在\n的后面再加\,表示 本次操作还没结束,用于在print中打印需要换行的代码,
e.g
print("it is not even a word\n\
you look cute in bubbles")
输出:
it is not even a word
you look cute in bubbles
3、长字符串:使用三引号,可以是三个单引号,也可以是三个双引号,前后呼应即可,在长字符串中换行不需要转义\n
e.g
print("""you look cute in bubbles
you are just all liquored up""")
输出:
you look cute in bubbles
you are just all liquored up
4、字符串加法和乘法:
e.g'520'+'37'
输出:
'52037'
e.g print("i love friends\n"*5)
输出:
i love friends
i love friends
i love friends
i love friends
i love friends
5、作业:
》给变量命名,要避免与 Python 内部使用的名字冲突,不然这个内置函数就废掉了
》 fishc = r"""
___ ___ ___ ___
/\\ ___ /\\ /\__\ /\\
/::\\ /\\ /::\\ /:// /::\\
/:/\:\\ \:\\ /:/\ \\ /:/__/ /:/\:\\
/::\~\:\\ /::\__\ _\:\~\ \\/::\\ ___/:/\:\\
/:/\:\ \:\__\ __/:/\/__//\ \:\ \ \__\/:/\:\/\__\/:/__/ \:\__\
\/__\:\ \/__//\/:// \:\ \:\ \/__/\/__\:\/://\:\\\/__/
\:\__\\::/__/ \:\ \:\__\ \:://\:\\
\/__/ \:\__\ \:\/:// /:// \:\\
\/__/ \::// /:// \:\__\
\/__/ \/__/ \/__/
"""
由于字符串内容中存在反斜杠(\),为了其完整性,请务必使用原始字符串(在三引号前面加上 r),以后只要在输出内容中有反斜杠 就加r,避免歧义
》99乘法表:
//注意:
1、在输出结果时,要两次回车,因为有两个for训循环
2、 print(i,"×",j,"=",i * j,end=' ') print中逗号用来分隔变量
end用来确定执行一次print变量后的结束方式,默认用换行
若用空格结束 记得''两个单引号中间要加空格
在该print中i,j,i*j都是变量值,加引号的× = 都是字符串
sep 指定输出多个对象之间的分隔符,默认是空格(' ')
end 指定最后的结束符,默认是换行符('\n')
99乘法表:i表示9行,第一个print在j的循环内,完成一次j循环后,输出换行,改变i值,再次进入j循环
代码:
for i in range(1, 10):
for j in range(1, i+1):
print(i,"×",j,"=",i * j,end=' ')
print("\n")
1 × 1 = 1
2 × 1 = 2 2 × 2 = 4
3 × 1 = 3 3 × 2 = 6 3 × 3 = 9
4 × 1 = 4 4 × 2 = 8 4 × 3 = 12 4 × 4 = 16
5 × 1 = 5 5 × 2 = 10 5 × 3 = 15 5 × 4 = 20 5 × 5 = 25
6 × 1 = 6 6 × 2 = 12 6 × 3 = 18 6 × 4 = 24 6 × 5 = 30 6 × 6 = 36
7 × 1 = 7 7 × 2 = 14 7 × 3 = 21 7 × 4 = 28 7 × 5 = 35 7 × 6 = 42 7 × 7 = 49
8 × 1 = 8 8 × 2 = 16 8 × 3 = 24 8 × 4 = 32 8 × 5 = 40 8 × 6 = 48 8 × 7 = 56 8 × 8 = 64
9 × 1 = 9 9 × 2 = 18 9 × 3 = 27 9 × 4 = 36 9 × 5 = 45 9 × 6 = 54 9 × 7 = 63 9 × 8 = 72 9 × 9 = 81
页:
[1]