python中print函数的运用
- 输出字符串
- 输出数字
- 输出带有运算符的表达式
- 输出数据到文件中(在D盘创建文件并输入内容)
- 进行不换行输出
i= 533
print(i)
print(‘hello world’)
print(“hello world”) #单引号和双引号功能相同
print(8999)
print(8+9) #输出含运算符的表达式
str1=‘my_fucking_life’
print(str1)
ex1=open(‘D:/text.txt’,‘a+’) #a+表示如果不存在文件就创建,存在就在后面添加
print(“helloworld”,file=ex1)
ex1.close()
print(‘hellomypython’,‘python’) #不换行输出,用逗号连接
转义字符
print(‘hello\nworld’) \n 表示换行
print(‘hello123\tworld’) \t表示 (占满制表位时)间隔四个字符位置
print(‘hello\tworld’) \t表示 间隔三个字符位置,因为o占了一个位置
print(‘hello123\rworld’) \r 表示return,从字符串首位开始写
print(‘hello123\bworld’) \b表示backspace,回退一位
print(‘http:\baidu.com’) 输出为http:\baidu.com
print(‘http:\\baidu.com’) 输出为http:\baidu.com,双反斜杠代表一个反斜杠
print(‘teacher said’good morning’’) 输出teacher said’good morning’
print(‘teacher said"good morning"’) 输出teacher said"good morning"
具体转义字符意义
当不需要把转义字符的意义表达时,就是使用原字符,在字符串前加r或R
print(r’hello\nworld’) 其中最后一个字符不能是反斜线,出现时会报错,如:
print(r’hello\nworld’)