首页 > 编程语言 >Python多行字符串

Python多行字符串

时间:2022-09-27 10:13:20浏览次数:87  
标签:多行 string Python 字符串 JournalDev using

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.com
JournalDev 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 string join() function. 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.

我们还可以使用string join()函数将字符串分成多行。 请注意,在方括号或反斜杠中,我们必须自己注意空格,如果字符串确实很长,则检查空格或双倍空格可能是一场噩梦。 我们可以使用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)

GitHub RepositoryGitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/23864/python-multiline-string

 

标签:多行,string,Python,字符串,JournalDev,using
From: https://www.cnblogs.com/wpcnblog/p/16733532.html

相关文章

  • python中利用smtplib发送邮件的3中方式 普通/ssl/tls
    #!/usr/bin/python#coding:utf-8importsmtplibfromemail.MIMETextimportMIMETextfromemail.Utilsimportformatdatefromemail.HeaderimportHe......
  • Linux升级python至3.x
    前言云服务器一般都用Linux系统,现在云服务器的Linux一般自带python,只是版本是2.x,比较老的那种。大部分人用的python应该都是3.x版本的,这时候你在本地编写的python文件拿到......
  • Python第四章实验报告
    一、实验题目Python第四章实例和实战作业二、实验目的和要求1.熟悉Pycharm的运行环境2.学习并掌握Python的流程控制语句三、主要仪器设备联想小新air15硬件:AMDR75......
  • 羊了个羊,但是Python简(li)单(pu)版
    大家好,欢迎来到Crossin的编程教室!要说最近最热门的游戏,那肯定是《羊了个羊》没跑了。连续上了好几天热搜,火到连央视都来提醒谨防有人利用游戏之名诈骗。但游戏爆火的另......
  • The Python Standard Library by Example pdf
    高清扫描版下载链接:https://pan.baidu.com/s/1XrEGFnmV_jwtkXILXLjUQg点击这里获取提取码 ......
  • Python源码剖析 pdf
    高清扫描版下载链接:https://pan.baidu.com/s/12DltvfQr4Tc7ZwhiG1UmAg点击这里获取提取码 ......
  • python
    基本数据类型之布尔值bool用来判断事物的对错是否可行只要用于流程控制中只有两种状态true对的真的可行的false错的假的不可行的python中所有......
  • python Thread
    #foriinrange(10):#t1=Thread(target=函数,args=(,))#t.append(t1)##t1.start()#forhint:#h.join()#共耗时:26.087260484695435500#fo......
  • Python-标识符命名规则
    合法标识符在Python中,标识符由字母,数字,下划线组成,但不能以数字开头.命名规则lower_underscore:小写字母中间隔下划线.一般使用小写加下划线UPPER_UNDERS......
  • 世界坐标系转换/相机标定/畸变矫正-Python
    PnP(Perspective-n-Point)是求解3D到2D点的对应方法。不论是相机和雷达的标定还是相机和相机的标定都可以使用PNP来解决,即通过不同坐标系下相同的点对求解变换矩阵。这里......