Python 的三引号字符串是如何工作的? [英] How does Python's triple-quote string work?

查看:46
本文介绍了Python 的三引号字符串是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个函数应该如何改成返回"123456"?

How should this function be changed to return "123456"?

def f():
    s = """123
    456"""
    return s

更新:每个人,问题在于理解如何在多行注释时没有 \t 或其他任何东西,而不是如何使用 re 模块.

UPDATE: Everyone, the question is about understanding how to not have \t or whatever when having a multiline comment, not how to use the re module.

推荐答案

如果您不想要额外的空格、制表符和换行符,请不要使用三引号字符串.

Don't use a triple-quoted string when you don't want extra whitespace, tabs and newlines.

使用隐式延续,更优雅:

Use implicit continuation, it's more elegant:

def f():
    s = ('123'
         '456')
    return s

这篇关于Python 的三引号字符串是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆