Force repr() to use single quotes

前端 未结 3 630
孤独总比滥情好
孤独总比滥情好 2020-12-19 17:31

I have a question, is there a way to \"force\" repr() to create always single quotes around a string?

This happens when I only use repr()

3条回答
  •  情话喂你
    2020-12-19 18:00

    Well, if your object is always a string you could do this:

    def repr_single(s):
        return "'" + repr('"' + s)[2:]
    
    print repr_single("test'")
    'test\''
    

    But as Martijn Pieters asked I'm curious as to your use case here.

提交回复
热议问题