python - 艰苦学习 Python 示例 11

标签 python

我正在学习困难方法中的示例 11,我只是好奇如何对高度部分使用转义序列。如果我询问多高,有人输入 6'2"并用英尺和英寸作为引号,我如何在其中输入一些在运行时不会显示反斜杠的内容?当你运行它时,原始输入会打印出来,例如 6\'2"。我已经尝试了一些方法,但没有任何效果。

    print "How tall are you?",
    height = raw_input()

最佳答案

在本练习中,您需要调用从 raw_input 获得的信息(变量“height”现在的值为“6'2”')并使用 %r 将其打印在句子中.

您可以使用不同的“%”类型,并且 %r 将为您提供字符串的“原始”表示形式。 Zed 已经要求您弄清楚 %r%s%d 之间的区别,所以您应该熟悉这一点。如果没有,请在此处阅读更多内容:

What's the difference between %r, %s and %d in python?

What does %s mean in Python?

要回答您的问题,这应该没有问题:

print "How tall are you?"
height = raw_input()

print "You are %s tall" % height

关于python - 艰苦学习 Python 示例 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34647587/

相关文章:

python - 在 tf.keras 中使用 tensorflow eager execution 时出现警告 `tried to deallocate nullptr`

python - 从 Django-admin 中删除默认应用程序

python - HyperlinkedModelSerializer 在 django rest 框架中使用 auth.User 抛出 ImproperlyConfigured 错误

python - 有没有更好的方法来使用 sam var 多个字符串模格式化?

python - For循环(新手)

python - 从 ELF 二进制文件中提取函数字节

python - 用Nuitka编译任何Python程序后,我得到 “is not a valid win32 application”错误

python - Python 中 Unicode 字符的显示问题(组合字符、假名字符等)

python - 在 Python 中,dict.pop(a,b) 是什么意思?

python - 有没有办法使用生存时间装饰器来缓存 Python 3.5 定义?