博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python多行字符串
阅读量:2532 次
发布时间:2019-05-11

本文共 2442 字,大约阅读时间需要 8 分钟。

Sometimes we have a very long string and we want to write it into multiple lines for better code readability. Python provides various ways to create multiline strings.

有时我们有一个很长的字符串,我们想将其写成多行以提高代码的可读性。 Python提供了多种创建多行字符串的方法。

使用三引号的Python多行字符串 (Python Multiline String using triple quotes)

If your long string has newline characters, then you can use triple quotes to write them into multiple lines. Note that anything goes inside triple quotes is the string value, so if your long string has many newline characters then you can use it to split them into multiple lines.

如果您的长字符串包含换行符,则可以使用三引号将它们写成多行。 请注意,三引号中包含的所有内容都是字符串值,因此,如果长字符串包含许多换行符,则可以使用它将它们分成多行。

Let’s say we have a long string as follows:

假设我们有一个长字符串,如下所示:

s = 'My Name is Pankaj.\nI am the owner of JournalDev.com\nJournalDev is a very popular website in Developers community.'

We can write it using triple quotes as follows:

我们可以使用三引号将其编写如下:

s = """My Name is Pankaj.I am the owner of JournalDev.comJournalDev is a very popular website in Developers community."""

But what if the string doesn’t have newline characters, then there are other ways to write them in multiple lines.

但是,如果字符串没有换行符,那还有其他方法可以将它们写成多行。

使用括号的多行字符串 (Multiline string using brackets)

We can split a string into multiple lines using brackets.

我们可以使用方括号将字符串分成多行。

s = ("My Name is Pankaj. "     "I am the owner of JournalDev.com and "     "JournalDev is a very popular website in Developers community.")print(s)

Output:

输出:

My Name is Pankaj. I am the owner of JournalDev.com and JournalDev is a very popular website in Developers community.

使用反斜线的多行字符串 (Multiline string using backslash)

s = "My Name is Pankaj. " \    "I am the owner of JournalDev.com and " \    "JournalDev is a very popular website in Developers community."print(s)

使用join()的Python多行字符串 (Python multiline string using join())

We can also split a string into multiple lines using . Note that in brackets or backslash way, we have to take care of spaces yourself and if the string is really long then it can be a nightmare to check for spaces or double spaces. We can get rid of that using join() function as shown below.

我们还可以使用将字符串分成多行。 请注意,在方括号或反斜杠中,我们必须自己注意空格,如果字符串确实很长,则检查空格或双倍空格可能是一场噩梦。 我们可以使用join()函数消除它,如下所示。

s = ' '.join(("My Name is Pankaj. I am the owner of",              "JournalDev.com and",              "JournalDev is a very popular website",              "in Developers community."))print(s)
. 检出完整的python脚本和更多Python示例。

翻译自:

转载地址:http://gaqzd.baihongyu.com/

你可能感兴趣的文章
B. Inna and Nine
查看>>
建议性列表输入文本框
查看>>
RTSP 资料
查看>>
[转]uboot中SPL作用
查看>>
Excel VBA Range对象基本操作应用示例
查看>>
html5拖拽
查看>>
kerboros安装
查看>>
我的学习之路_第二十九章_bootstrap
查看>>
Python读取文件行数不对
查看>>
考研经验交流
查看>>
手游助手应用源码项目
查看>>
职场心得笔记
查看>>
Android context(Application/Activity)与内存泄露
查看>>
mysql 行转列
查看>>
jquery easyui 经验
查看>>
Kafka官方文档翻译——设计
查看>>
本地推送
查看>>
免费的在线文档翻译神器
查看>>
RabbitMQ --- Publish/Subscribe(发布/订阅)
查看>>
细思极恐-你真的会写java吗
查看>>