打印包含字符串和其他 2 个变量的变量 [英] printing variable that contains string and 2 other variables

查看:23
本文介绍了打印包含字符串和其他 2 个变量的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var_a = 8
var_b = 3

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

当我运行程序时,var_c 被打印出来如下:('hello my name is:', 8, 'and', 3, 'bye') 但是所有的括号等也被打印出来,为什么这是和有没有办法摆脱这些符号?

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?

如果我这样运行程序:

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

我没有这个问题

推荐答案

您可以 格式化您的字符串以获得您预期的字符串输出.

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.

这篇关于打印包含字符串和其他 2 个变量的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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