简体   繁体   English

打印包含字符串和其他2个变量的变量

[英]printing variable that contains string and 2 other variables

var_a = 8
var_b = 3

var_c = "hello my name is:",var_a,"and",var_b,"bye"
print(var_c)

When I run the program var_c gets printed out like this: ('hello my name is:', 8, 'and', 3, 'bye') but all the brackets etc get printed as well, why is this and is there a way to get rid of those symbols? 当我运行程序时,var_c被打印出来像这样:('你好我的名字是:',8,'和',3,'再见')但是所有的括号等也被打印出来,为什么会这样呢?摆脱那些符号的方法?

If I run the program like this: 如果我像这样运行程序:

print("hello my name is:",var_a,"and",var_b,"bye")

I don't have that problem 我没有那个问题

You can format your string to get your expected string output. 您可以格式化字符串以获得预期的字符串输出。

var_c = "hello my name is: {} and {}, bye".format(var_a, var_b)

As commented, your existing output is due to the variable being returned as a tuple, whereas you want it as one string. 如评论所示,您的现有输出是由于变量作为元组返回,而您希望它作为一个字符串。

在Python 3.6+中,您可以使用新的f-strings( 格式化的字符串文字 ):

var_c = f"hello my name is: {var_a} and {var_b}, bye"

var_c is actually a tuple, so print interprets it like that and you get its representation printed. var_c实际上是一个元组,因此print会像这样解释它并打印出它的表示。

var_a = 8
var_b = 3
var_c = "hello my name is:", var_a, "and", var_b, "bye"

but you could just tell print to use the tuple as arguments with * 但你可以告诉print使用元组作为参数*

print(*var_c)

result: 结果:

hello my name is: 8 and 3 bye

(of course this is theorical, it's better to use str.format as other answers said) (当然这是理论上的,最好使用str.format作为其他答案说)

您应该将var_c创建为字符串,如下所示

var_c = "hello my name is: %s and %s bye" % (var_a, var_b)
var_c = "hello my name is:",var_a,"and",var_b,"bye"

with this line, you are making var_c as tuple... to make it string make it like 使用这一行,你将var_c作为元组...使它成为字符串

var_d = "hello my name is:%s and %s bye" % (var_a,var_b)
print(var_d)

and it will output 它会输出

hello my name is:8 and 3 bye

Your program is creating a tuple and you print the tuple: 你的程序正在创建一个元组并打印元组:

var_a = 8
var_b = 3

var_c = "hello my name is:", var_a, "and", var_b, "bye"
print(var_c)

output: 输出:

('hello my name is:', 8, 'and', 3, 'bye') 

Alternatively print like this: 或者像这样打印:

for item in var_c:
    print(item+' ', end='')

output: 输出:

hello my name is: 8 and 3 bye

That's because you are using syntax to create a tuple! 那是因为你使用语法来创建一个元组!

tup = "a", "b", "c", "d"

Refer this : https://www.tutorialspoint.com/python/python_tuples.htm . 请参阅: https//www.tutorialspoint.com/python/python_tuples.htm

If you just want to concatenate these you can write: 如果你只想连接这些,你可以写:

var_c = "hello my name is: " + str(var_a) + " and " + str(var_b) + " bye"
var_a = 8
var_b = 3

var_c = "hello my name is:", var_a, "and", var_b, "bye"
print(var_c)

output: 输出:

('hello my name is:', 8, 'and', 3, 'bye') 

Alternatively print like this: 或者像这样打印:

for item in var_c:
    print(item+' ', end='')

output: 输出:

hello my name is: 8 and 3 bye

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

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