如何删除不需要的单引号 [英] How to remove unwanted single quotes

查看:76
本文介绍了如何删除不需要的单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,偶然发现了一些难看的东西,我想删除它.这是我的代码:

I'm new to programming and I stumbled across something unsightly that I would like to remove. Here's my code:

def test1():
    print "What is your name, traveler?"
    name = raw_input()
    print "Hi %r!" % (name)

输出如下:

What is your name traveler?
Hi 'Karatepig'!

有什么办法可以去掉我名字周围的那些单引号吗?

Is there any way to get rid of those single quotes around my name?

推荐答案

你想在那里使用 %s,而不是 %r.

You want to use %s there, not %r.

print "Hi %s!" % (name)

我会添加细节,但我认为其他人已经写了更好的解释这里此处.

I'd add details, but I think others have already written better explanations here and here.

最突出的是:

%s 和 %r 的区别在于 %s 使用了 str 函数,而%r 使用 repr 函数.您可以阅读有关两者之间的差异这个答案中的 str 和 repr ,但对于内置类型,最大的实践中的区别在于 repr 字符串包括引号 和所有特殊字符都被转义.

The difference between %s and %r is that %s uses the str function and %r uses the repr function. You can read about the differences between str and repr in this answer, but for built-in types, the biggest difference in practice is that repr for strings includes quotes and all special characters are escaped.

这篇关于如何删除不需要的单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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