【问题标题】:What is the most efficient way to strip all spaces and quotes from a string with python? [duplicate]用python从字符串中去除所有空格和引号的最有效方法是什么? [复制]
【发布时间】:2012-08-23 19:17:51
【问题描述】:

可能重复:
Best way to strip punctuation from a string in Python

我有一个字符串要放在输出"{{ var }}" 的两个双引号之间,所以我想确保从用户提供的字符串的末尾删除所有单/双引号。实现这一目标的最有效方法是什么?

输入/期望输出:

   """""""""" string here '''''''''''''''         => 'string here'
       string'''''''""""""''''''"""""  => 'string'

【问题讨论】:

  • 希望看到一个副本。找到的所有内容都没有指定多个字符实例。

标签: python regex string


【解决方案1】:

strip 与包含您希望从末尾删除的字符的字符串一起使用:

s = s.strip(' "\'\t\r\n')

【讨论】:

    【解决方案2】:
    from string import whitespace
    new_string = string.strip(whitespace + '"\'')
    

    使用strip 方法和whitespace 常量开始您要删除的字符列表。

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 2022-03-07
      • 1970-01-01
      • 2019-10-12
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      相关资源
      最近更新 更多