print函数
可以输出数字
print(520)
print(98.5)
可以输出字符串
print(‘hello world‘)
print("hello world")
print("{0} {1}".format("hello","world"))
print("%s %s"%("hello","world"))
可以输出含有运算符的表达式
print(3+1)
print(8*1)
可以输出到文件
fp = open(‘D:/text.txt‘,‘a+‘)
print(‘hello world‘,file=fp)
fp.close
注意:1.所指定的盘符必须存在;2.使用 file=fp;3.a+表示文件不存在就创建,存在的话就在内容上继续追加。
转义字符
转义字符
print(‘hello \n world‘) #\ +转义功能的首字母 n--newline的首字母表示换行
print(‘hello \t world‘) #\t 水平制表符,一组4个空格的位置
print(‘helloooo \t world‘)
print(‘hello \r world‘) #\r 回车把hello覆盖
print(‘hello \b world‘) #\b 退一个格将o退没了
print(‘http:\\\\www.baidu.com‘) #\\输出一个\,\\\\输出2个print(‘ 老师说:\’大家好\‘ ‘)
原字符
不希望字符串中的转义字符起作用,字符串前加上r或者R,常用于文件路径前,防止路径转义,或字符串不想被转义时
print(r"{0}\n{1}".format("hello","world"))
print(r"%s\t%s"%("hello","world"))
注意事项:最后一个字符不能是反斜杠\,但是是两个\可以。
原文:https://www.cnblogs.com/happy-winds/p/14757760.html