python学习(一)

发布时间 2023-03-29 22:27:10作者: 喵喵是只猫

python基础(一)

1print()函数

1.1print()输出可以是数字

代码:

print(520)
print(87.1)

结果:

520
87.1

print()可以输出字符串
代码:

print('hello world')
print("hello world")

结果:

hello world
hello world

print()中可含有运算符表达式
代码:

print(1+3)

结果:

4

1.2 print可将数据输出文件中

代码:

fp=open('D:/text.txt','a+')    #open('文件路径','a+')a是指一读写方式执行文件,若是文件存在,则直接写入,若是文件不存在,则新建文件再写入
print('hello world',file=fp)
fp.close()

结果:

![](https://images.cnblogs.com/cnblogs_com/blogs/774932/galleries/2293775/t_230329105221_%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230329185016.png)
![](https://images.cnblogs.com/cnblogs_com/blogs/774932/galleries/2293775/t_230329105229_%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230329185029.png)

1.3 不进行换行输出(输出内容在一行中)

代码:

print('hello','world','Python')

结果:

hello world Python

2转义字符

反斜杠+想要实现的转义功能的首字母
\n换行
\t一个制表位
\r回车
\b退格
代码:

print('hello\nworld')
print('hello\tworld')   #四个字符一个制表位
print('hello\rworld')
print('hello\bworld')
print('hello\\world')
print('\'helloworld\'')

结果

hello                   #\n
world
hello	world           #\t
world                   #\r
hellworld               #\b
hello\world             #\\
'helloworld'            #\'

2.1 原字符,不想转义字符发生作用,在字符串前加上r或R

代码:

print(r'hello\nworld')
#注意!!!最后一位不能是反斜线,会报错
print(r'hello\nworld\')
print(r'hello\nworld\\')

结果:

hello\nworld
报错
hello\nworld\\