Python,字符串格式,换行符(C++ - std::endl) [英] Python, string format, newline (C++ - std::endl)

查看:104
本文介绍了Python,字符串格式,换行符(C++ - std::endl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我格式化字符串(在重载运算符 str 中)并且我不想使用原始的 \n 或 \r\n 标签.在 C++ 中有像 std::endl 这样的 python 跨平台换行符标识符吗?

我试着用谷歌搜索,但没有找到答案.

解决方案

如何使用 os.linesep?它包含适用于您的操作系统的行分隔符:

<预><代码>>>>导入操作系统>>>os.linesep'\n'>>>打印第一行"+ os.linesep +第二行"第一行第二行

I format string (in overloading operator str) and i don't want to use raw \n or \r\n tags. Have python cross-platform newline identifier like std::endl in C++?

I trying to google it, but don't found answer.

解决方案

How about using os.linesep? It contains the appropriate line separator for your OS:

>>> import os
>>> os.linesep
'\n'
>>> print "line one" + os.linesep + "line two"
line one
line two

这篇关于Python,字符串格式,换行符(C++ - std::endl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆