版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/PoGeN1/article/details/84137711
一、\n
换行命令
定义 text 为字符串, 并查看使用 \n 和不适用 \n 的区别:
>>> text='this is first line,this is second line,this is third line'
>>> print(text)
this is first line,this is second line,this is third line
>>> text='this is first line\n,this is second line\n,this is third line\n'
>>> print(text)
this is first line
,this is second line
,this is third line
# 输入换行命令\n,要注意斜杆的方向。注意换行的格式和c++一样
二、open 读文件方式
使用 open
能够打开一个文件, open
的第一个参数为文件名和路径 ‘my file.txt’,
第二个参数为将要以什么方式打开它, 比如 w
为可写方式.
如果计算机没有找到 ‘my file.txt’
这个文件, w
方式能够创建一个新的文件, 并命名为 my file.txt
if __name__=='__main__':
text='hello world'
# 用法: open('文件名','形式'), 其中形式有'w':write;'r':read.
my_file = open('my file.txt', 'w')
my_file.write(text) # 该语句会写入先前定义好的 text
my_file.close() # 关闭文件
三、\t tab 对齐
使用 \t 能够达到 tab 对齐的效果:
if __name__=='__main__':
text='\thello world\n \thello \n \tworld\n'
my_file = open('my file.txt', 'w') # 用法: open('文件名','形式'), 其中形式有'w':write;'r':read.
my_file.write(text) # 该语句会写入先前定义好的 text
my_file.close() # 关闭文件
命令行中展示一下区别
有\t
>>> text='\thello world\n \thello \n \tworld\n'
>>> print(text)
hello world
hello
world
没有\t
>>> text='hello world\nhello\nworld\n'
>>> print(text)
hello world
hello
world