推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
PhanKiap
V2EX  ›  Python

字符串太长不想写在一行上,能否写成多行

  •  
  •   PhanKiap · Oct 12, 2015 · 11376 views
    This topic created in 4044 days ago, the information mentioned may be changed or developed.
    long("长长长长
          长长
          长")
    

    保持缩进

    29 replies  •  2015-10-14 11:43:10 +08:00
    cylin
    cylin
    Oct 12, 2015
    1
    print("hello" +
    " " +
    "world")

    str="hello"
    str+=" "
    str+="world"
    print(str)
    jky
    jky
    Oct 12, 2015 via Android❤️1
    2
    S = ("长长长"
    "长长长")
    oyasmi
    oyasmi
    Oct 12, 2015
    3
    能

    long("长长长长"
    "长长"
    "长")
    loading
    loading
    Oct 12, 2015 via iPhone
    4
    @cylin
    @jky
    @oyasmi

    听说过 三引号
    loading
    loading
    Oct 12, 2015 via iPhone
    5
    搜索 python 三引号
    jky
    jky
    Oct 12, 2015 via Android
    6
    @loading 三引号似乎不能保持缩进?
    v2gba
    v2gba
    Oct 12, 2015
    7
    print("""
    12345
    ++++6
    ++++++++7
    """)
    14
    14
    Oct 12, 2015❤️1
    8
    aec4d
    aec4d
    Oct 12, 2015
    9
    当做函数参数引用的时候用 8 楼 因为美观
    不当参数的时候用一楼的第二种 个人认为美观
    仅仅因为太长不想写在一行 那么你万万不要用三行字符串
    因为
    """a
    b""" != 'ab'
    beibeijia
    beibeijia
    Oct 12, 2015❤️1
    10
    楼上正解 python 编程风格指南里有提到的 楼主可以去看看 附链接: http://docs.python-guide.org/en/latest/writing/style/#line-continuations
    beibeijia
    beibeijia
    Oct 12, 2015
    11
    额 我说的是 8 楼。==!
    kifile
    kifile
    Oct 12, 2015
    12
    fmt.Println(`常常常常
    就是这么长`)
    aliipay
    aliipay
    Oct 12, 2015
    13
    @14 为毛我的提示 IndentationError: unexpected indent ,在换行的地方
    C0VN
    C0VN
    Oct 12, 2015
    14
    难道不是行尾加一个 \ 就可以换行了?
    Delbert
    Delbert
    Oct 12, 2015 via Android
    15
    行尾加换行符\
    啊
    aec4d
    aec4d
    Oct 12, 2015
    16
    @xavierskip
    @Delbert
    def foo():
    a = '1\
    2'
    print a

    foo()
    打印出来的不是 12 我以前被坑过-_-
    lxy42
    lxy42
    Oct 13, 2015
    17
    @aec4d a = '1' + \
    '2'
    bigcoon
    bigcoon
    Oct 13, 2015 via iPhone
    18
    我觉得出现长字符串都是代码结构有问题
    C0VN
    C0VN
    Oct 13, 2015
    19
    @aec4d 再函数中确实是。那就这样吧, pycharm 生成的写法就是这样

    zonghua
    zonghua
    Oct 13, 2015 via iPhone
    20
    @xavierskip 这是把换行符转义了
    java javascript 的话一般都是用 +
    pandada8
    pandada8
    Oct 13, 2015
    22
    bazingaterry
    bazingaterry
    Oct 13, 2015
    23
    用\不就可以了?
    cxbig
    cxbig
    Oct 13, 2015
    24
    长字符串这种东西不是应该放在 i18n 之类的翻译文件里的么?
    582033
    582033
    Oct 13, 2015 via Android
    25
    定界符
    aec4d
    aec4d
    Oct 13, 2015
    26
    @xavierskip 哇哦 感谢提醒 自己搞错了
    gamexg
    gamexg
    Oct 13, 2015❤️1
    27
    pycharm 写字符串觉得长了直接一个回车,单引号时 IDE 会自动处理的。
    shuiniushushu
    shuiniushushu
    Oct 13, 2015
    28
    js 有继行符
    "abc\
    xxx\
    bcd"
    其他语言不知道有没有
    okletswin
    okletswin
    Oct 14, 2015
    29
    python pep8 autoformat
    好多 IDE 都支持此插件, 文件保存时自动完善格式,自动拆行
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1756 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 94ms · UTC 03:35 · PVG 11:35 · LAX 19:35 · JFK 22:35
    ♥ Do have faith in what you're doing.