返回顶部
首页 > 资讯 > 后端开发 > Python >【python】代码换行的几种方法
  • 805
分享到

【python】代码换行的几种方法

换行几种方法代码 2023-01-31 03:01:52 805人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

代码太长怎么办,反斜杠\引号""" ‘’'来帮忙! 在写list或者较长的字符串时候,或者多个循环造成IDE不够用时,就需要代码换行了。主要的代码换行有通用的反斜杠\和针对字符串起作用的三引号结构。 1.反斜杠 对于一般表达式

代码太长怎么办,反斜杠\引号""" ‘’'来帮忙!

在写list或者较长的字符串时候,或者多个循环造成IDE不够用时,就需要代码换行了。主要的代码换行有通用的反斜杠\和针对字符串起作用的三引号结构。

1.反斜杠

对于一般表达式来说,反斜杠后直接回车即可实现续行,使用的关键在于反斜杠后不能用空格或者其他符号。

a = 1
b = 2
c = a +\
b
print(c)

>>> 3

longlist = ['3D','3-D','3d','3-d','three-dimensions','Three-Dimensions','Three Dimensions','THREE DIMENSIONS','geometry',\
       'Geometry','GEOMETRY','Geometric','surface','Surfaces','Surface','SURFACE',\
       '3D Pose Estimation','Pose','POSE','POINTCLOUD']
print(longlist)

>>> ['3D', '3-D', '3d', '3-d', 'three-dimensions', 'Three-Dimensions', 'Three Dimensions', 'THREE DIMENSIONS', 'geometry', 'Geometry', 'GEOMETRY', 'Geometric', 'surface', 'Surfaces', 'Surface', 'SURFACE', '3D Pose Estimation', 'Pose', 'POSE', 'POINTCLOUD']

对于字符串也有同样的效果

longstring = 'this is a long long long long long long long \
string'
print(longstring)

>>> this is a long long long long long long long string

2.三引号

longstring1 = '''this is a long long long long long long long 
string'''
print(longstring1)
longstring2 = """this is another long long long long long long long 
string"""
print(longstring2)

this is a long long long long long long long string this is another long long long long long long long string
还可以实现ascii字符输出呢:

print('''
   _____                   .__ .__     _____            __   
  /  _  \    ______  ____  |__||__|   /  _  \ _______ _/  |_ 
 /  /_\  \  /  ___/_/ ___\ |  ||  |  /  /_\  \\_  __ \\   __\-  
/    |    \ \___ \ \  \___ |  ||  | /    |    \|  | \/ |  |  
\____|__  //____  > \___  >|__||__| \____|__  /|__|    |__|  
        \/      \/      \/                  \/               
''')
#注意art中的反斜杠被动换行
>>> 
   _____                   .__ .__     _____            __   
  /  _  \    ______  ____  |__||__|   /  _  \ _______ _/  |_ 
 /  /_\  \  /  ___/_/ ___\ |  ||  |  /  /_\  \_  __ \   __\- 
/    |    \ \___ \ \  \___ |  ||  | /    |    \|  | \/ |  |  
\____|__  //____  > \___  >|__||__| \____|__  /|__|    |__|  
        \/      \/      \/                  \/               
print('''* bear *  11/96
        _
       (\\  _                      ___
      .-"`"(\\                _.""`   `"-.
     /      ` `-._        _.-"            `\__
    6   6)        `-.__.-'                    `",
   /                                         `;-`
  /     ,                                     |
 ()    /  /`                                  |
  `---`"~``\                                  |
            \                                 |
             \            \      /           /
             /`,   ,      |     |           /
            /   "-.|      |     |         /'
           /     / |     /,__   |       /`\ 
      jgs /    /'  |    /    `"'\      (   \ 
       __/   /'    |   |         `\     \   \ 
       \    /      |   |           `\    \   \ 
        `-,/      /    |            /     |-"`
                 `"""^^^           `^^""""`

------------------------------------------------
Thank you for visiting https://asciiart.WEBsite/
This ASCII pic can be found at
Https://asciiart.website//index.PHP?art=animals/bears
''')
>>> 
* bear *  11/96
        _
       (\  _                      ___
      .-"`"(\                _.""`   `"-.
     /      ` `-._        _.-"            `\__
    6   6)        `-.__.-'                    `",
   /                                         `;-`
  /     ,                                     |
 ()    /  /`                                  |
  `---`"~``\                                  |
            \                                 |
             \            \      /           /
             /`,   ,      |     |           /
            /   "-.|      |     |         /'
           /     / |     /,__   |       /`\ 
      jgs /    /'  |    /    `"'\      (   \ 
       __/   /'    |   |         `\     \   \ 
       \    /      |   |           `\    \   \ 
        `-,/      /    |            /     |-"`
                 `"""^^^           `^^""""`

------------------------------------------------
Thank you for visiting https://asciiart.website/
This ASCII pic can be found at
https://asciiart.website//index.php?art=animals/bears

ref:
https://blog.csdn.net/hllsmart/article/details/51236877
https://blog.csdn.net/qq_40229981/article/details/83587503
https://blog.csdn.net/baihhzm/article/details/73433160
https://blog.csdn.net/G_66_hero/article/details/78745608
ascii art:
http://patorjk.com/software/taag/#p=display&h=1&v=3&f=Graffiti&t=Ascii Art
https://asciiart.website//index.php?art=animals/bears
http://www.asciiarts.net/
http://picascii.com/


在这里插入图片描述
pic from pexels.com

--结束END--

本文标题: 【python】代码换行的几种方法

本文链接: https://lsjlt.com/news/187558.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作