【问题标题】:switching between different quotation marks for output在输出的不同引号之间切换
【发布时间】:2018-01-22 13:29:40
【问题描述】:

如何让我的代码更改输出的引号。我看到一些提到 json 的参考资料,但我想我需要自己写。 所以我会发布问题,然后是我的代码:

程序:quote_me() 函数

quote_me 接受一个字符串参数并返回一个字符串,如果打印出来,该字符串将用双引号括起来

检查传递的字符串是否以双引号(“\””)开头,然后用单引号包围字符串 如果传递的字符串以单引号开头,或者不以引号开头,则用双引号括起来

测试将字符串输入作为参数传递给quote_me()的函数代码

[ ] 创建并测试 quote_me()

def quote_me(单词):

if word == ("\'"):

    str(word).replace ("\'", '\"')

else:
    return word

print(quote_me(input("这是什么句子:")))

也许我也误解了需要什么,如果是这样,请告诉。

【问题讨论】:

    标签: python-3.x


    【解决方案1】:
    def quote_me(word):
        if word.startswith("\"") and word.endswith("\""):
            word = word.replace("\"","\'")
        elif word.startswith("\'") and word.endswith("\'"):
            word = word.replace("\'","\"")
        else:
            word = "\""+word+"\""
        return word
    

    【讨论】:

    • @issaahmed 如果这是正确的答案,它应该被投赞成票。
    猜你喜欢
    • 2017-03-30
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2020-10-10
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多