python的print函数和转义字符

python中print函数的运用

  1. 输出字符串
  2. 输出数字
  3. 输出带有运算符的表达式
  4. 输出数据到文件中(在D盘创建文件并输入内容)
  5. 进行不换行输出

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’)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值