Fork me on GitHub

学习python -- 第001天

一、输出函数的运用:

print()函数的使用
·print()函数可以输出哪些内容?
·(1)print)函数输出的内容可以是数字
·(2)print)函数输出的内容可以是字符串
·(3)print)函数输出的内容可以是含有运算符的表达式
·print()函数可以将内容输出的目的地
·(1)显示器
·(2)文件
·print()函数的输出形式
·(1)换行
·(2)不换行

实例代码:

 

 1 #
 2 # @author:浊浪
 3 # @time: 2021/1/10 10:11
 4 #输出数字
 5 print(100)
 6 print(98.5)
 7 
 8 #输出字符串
 9 print('helloworld')
10 print("helloworld")
11 print('''helloworld''')
12 
13 #输出含有运算符的表达式
14 print(3+1)
15 
16 
17 #输出到文件上
18 fp = open('D:/text.txt', 'a+') #如果不存在就创建文件,a+表示在其后面添加
19 print('helloworld', file=fp) #需要指定文件名
20 fp.close()
21 ##
22 fp = open('text.txt', 'a+')#创建在当前文件夹
23 print('helloworld', file=fp)
24 fp.close()
25 
26 #不换行输出
27 print('hello', 'world', 'Python')

 

二、转义字符

.开么是转义字符呢?
·就是反斜杠+想要实现的转义功能首字母。
·为什么需要转义字符?
·当字符串中包含反斜杠、单引号和双引号等有特殊用途的字符时,必须使用反斜杠对这些字符进行转义(转换一个含义)

反斜杠:\\

单引号:\'

双引号:\"


当字符串中包含换行、回车,水平制表符或退格等无法直接表示的特殊字符时,也可以使用转义字符当字符串中包含换行、回车,水平制表符或退格等无法直接表示的特殊字符时,也可以使用转义字符

换行:\n

回车:r

水平制表符:\t

退格:\b

 

实例代码:

 1 #
 2 # @author:浊浪
 3 # @time: 2021/1/10 10:32
 4 # 转移字符
 5 print('hello\nworld')  # n => newline的首字母
 6 print('hello\tworld')
 7 print('hellooo\tworld')  # 制表字符 按4个作为一个制表符 满了就重开,没满就填满
 8 print('hello\rworld')  # return 移动到本行的开头 覆盖掉前面的字符串
 9 print('hello\bworld')  # 退格一个字符
10 
11 print('https:\\\\www.baidu.com')
12 print('他会说:\'优美的中国话***\'')
13 
14 # 原字符,不希望字符串中的转义字符起作用,就使用原字符,就是在字符串之前加上r,或R
15 print(r'hello\nworld')
16 #但是最后一个字符不能是单独的反斜杠 可以是双反斜杠
17 #这个会报错  : print(r'hello\nworld\')
18 print(r'hello\nworld\\')

 

posted @ 2021-01-10 23:12  走位,走位  阅读(83)  评论(0编辑  收藏  举报