【python】代碼換行的幾種方法

代碼太長怎麼辦,反斜槓\引號""" ‘’'來幫忙!php

在寫list或者較長的字符串時候,或者多個循環形成IDE不夠用時,就須要代碼換行了。主要的代碼換行有通用的反斜槓\和針對字符串起做用的三引號結構。python

1.反斜槓

對於通常表達式來講,反斜槓後直接回車便可實現續行,使用的關鍵在於反斜槓後不能用空格或者其餘符號。web

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

>>> 3app

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']svg

對於字符串也有一樣的效果ui

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

>>> this is a long long long long long long long stringthis

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字符輸出呢:spa

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/.net


在這裏插入圖片描述
pic from pexels.com3d

相關文章
相關標籤/搜索