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

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

在写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

现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。

分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值