Python中的多行字符串

Python supports different type and format strings. In this tutorial we will simply look multi line strings. We have following code and want to type it in single expression in multi line.

Python支持不同的类型和格式字符串。 在本教程中,我们将简单地查看多行字符串。 我们有以下代码,想在多行中的单个表达式中键入它。

逐行加入 (Joining Line By Line)

The most basic and simplest form is adding string lines step by step like below. We will use . in order to add strings together which will create a multi line string which is stored in a variable named $html in this example.

最基本,最简单的形式是逐步添加字符串,如下所示。 我们将使用. 为了将字符串添加在一起,这将创建一个多行字符串,该字符串在此示例中存储在名为$html的变量中。

html="<html><body>";
html.="</body></html>
echo $html

双引号 (Double Quote)

We can use double quote by wrapping multiple lines. This will legal for python programming. And we can end the string whenever we want like below.

我们可以通过换行来使用双引号。 这将适用于python编程。 而且我们可以在需要时随时结束字符串,如下所示。

html="<html><body>
</body></html>"

3单引号 (3 Single Quote)

By default single quote is used to express single line strings. But 3 single quote can be used to express multiline strings in Python programming language.

默认情况下,单引号用于表示单行字符串。 但是3个单引号可以用来表示Python编程语言中的多行字符串。

html='''<html><body><br>
</body></html>'''
3 Single Quote
3 Single Quote
3单引号

单线(Single Line)

Basic for is single line string. We can use single line Python strings like below where a double quote with a single line stored in a variable which is html in this example. We will use \n  which means new line.

基本的是单行字符串。 我们可以像下面这样使用单行Python字符串,其中在此示例中的html变量中存储有单引号的单引号。 我们将使用\n表示换行。

html="<html><body>\n<br></body></html>"

紧急行动 (EOF)

EOF is special keyword used to specify multi line strings. The start of the multi line string is marked with <<< EOF and then the string will be given. We also need to specify the end of the string with EOF too like below.

EOF是用于指定多行字符串的特殊关键字。 多行字符串的开头用<<< EOF标记,然后将给出该字符串。 我们还需要使用EOF指定字符串的结尾,如下所示。

html= <<< EOF
<html><body>
</body></html>
EOF

Python信息图表中的多行字符串 (Multiline Strings in Python Infographic)

Multiline Strings in Python Infographic
Multiline Strings in Python Infographic
Python信息图表中的多行字符串
LEARN MORE  How To Convert List To String in Python?
了解更多如何在Python中将列表转换为字符串?

翻译自: https://www.poftut.com/multiline-strings-python/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值