Python的print函数

1. 打印不换行


print函数默认是输出换行的如果想不换行可以这样

#!/usr/bin/env python
# coding=utf-8
# Python 2.7.3
str1 = u"输出换行"# Unicode编码
str2 = u"输出不换行"
print(str2),# 在后面增加一个逗号就不换行
print(str1)

2. 使用print输出到文件


#!/usr/bin/env python
# coding=utf-8
# Python 2.7.3
f = open('data.txt', 'w')
str1 = u"输出换行"# Unicode编码
str2 = u"输出不换行"
# 输出到文件
print >> f, str2.encode("utf8"),# 要转换为utf8编码, 否则写入文件报错
print >> f, str1.encode("utf8")
f.close()





你可能感兴趣的:(python,使用,print函数)