简体   繁体   English

如何在Python中的消息内用引号打印名称?

[英]How to print a name in quotes inside a message in Python?

I started this Python course today with no programming experience, and I can't figure this out (it's so easy but I can't seem to figure it out). 我今天是在没有任何编程经验的情况下开始这门Python课程的,所以我无法弄清楚(这很简单,但我似乎无法弄清楚)。

This is what the program should look like in the end: 程序最终应该是这样的:

What is your name? 你叫什么名字? 3von 3冯

So you call yourself '3von' huh? 所以你称自己为“ 3von”吧?

This is how I wrote it: 我是这样写的:

name = input("What is your name? ")

print('So you call yourself', (name), "huh?")

What I can't figure out is how to get the ' ' around the (name) without it turning into a string. 我不知道的是如何在(name)周围得到''而不将其转换为字符串。

Any help is appreciated! 任何帮助表示赞赏!

PS What are the (name) things called? PS什么叫(名称)?

You need to either enclose the other strings with different quotes, or escape them. 您需要用不同的引号将其他字符串引起来,或将它们转义。 Also, the default separator for print() is a space, which you don't want in between the quotes and name . 另外, print()的默认分隔符是一个空格,您不需要在引号和name之间。 Use + to concatenate it together, and keep in mind that this means you'll need to add the necessary spaces explicitly. 使用+将其连接在一起,并请记住,这意味着您需要显式添加必要的空格。

Different quotes: 不同的引号:

print("So you call yourself '" + name + "' huh?")

Escaped: 转义:

print('So you call yourself \'' +name + '\' huh?')

String formatting is also a good approach, although you'll still need to mind your quotes or use escapes: 字符串格式化也是一种好方法,尽管您仍然需要注意引号或使用转义符:

print("So you call yourself '{}' huh?".format(name))

The {} will be replaced with name . {}将替换为name String formatting has a powerful mini language and is a great tool to learn. 字符串格式化具有强大的迷你语言 ,是学习的绝佳工具。

name here is what is called a "variable". 这里的name是所谓的“变量”。

To substitute a variable into a string, the best thing to use is the format method on strings. 要将变量替换为字符串,最好使用字符串上的format方法。 See the examples in the documentation, linked above. 请参阅上面链接的文档中的示例。

In your case, you'd use: 就您而言,您将使用:

print "So you call yourself '{}' huh?".format(name)

The {} inside the string indicate the place where the substitution goes, and the arguments to format are the variables to put in that place. 字符串中的{}表示替换的位置, format的参数是放置在该位置的变量。

Note that the quotes around the outside of the string here are double quotes, and the ones inside, which you want printed, are single quotes. 请注意,此处字符串外部的引号是双引号,而要打印的内部引号是单引号。 Inside a double quoted string, single quotes are treated as part of the string, and vice-versa. 在双引号字符串内,单引号被视为字符串的一部分,反之亦然。

Assuming I'm reading your question correctly, you want the output to look like this: 假设我正确阅读了您的问题,您希望输出看起来像这样:

So your name is '3von' huh?

To do this, you need to both contain your single quotes within double quotes and concatenate your strings using +, like so: 为此,您既需要将单引号包含在双引号中,也需要使用+连接字符串,如下所示:

print("So your name is '" + name + "' huh?")

(name) is a variable, although I'm not sure that was what you were asking. (名称)是一个变量,尽管我不确定那不是您要的。 And you don't need to put it in parentheses. 而且您无需将其放在括号中。

You can use string formating. 您可以使用字符串格式。

name = input("What is your name? ")

print("So you call yourself '%s' huh?" % name)

试试这个,您在寻找这个吗

print('So you call yourself '+name+ " huh?")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM