Python 2.X 在字符串周围添加单引号

标签 python python-2.7

目前要在字符串周围添加单引号,我想出的最佳解决方案是制作一个小包装函数。

def foo(s1):
    return "'" + s1 + "'"

有没有更简单的 pythonic 方式来做到这一点?

最佳答案

这是另一个(可能更像 pythonic)选项,使用 format strings :

def foo(s1):
    return "'{}'".format(s1)

关于Python 2.X 在字符串周围添加单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22952543/

相关文章:

python - 累积一个orderedDict

python - 如何使用python发送nc类似命令?

python - 使用 boto3 清空 s3 存储桶的最快方法是什么?

python - Discord.py 添加角色

python - Celery 和 Django 简单示例

python - 是否可以在 python 中使用可选的 with/as 语句?

python 模块 BeautifulSoup importError

python - 为什么在比较Python对象元组时先调用__eq__,然后调用__cmp__?

python - 如何在 matplotlib 中制作地毯图

python - dx=(x>0)*dout x>0 在此代码中代表什么?