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,“bye”),但是所有的方括号等都打印出来了,为什么会这样,有没有办法去掉这些符号呢?
If I run the program like this:
如果我像这样运行程序:
print("hello my name is:",var_a,"and",var_b,"bye")
I don't have that problem
我没有这个问题。
8 个解决方案
#1
7
In Python 3.6+ you can use the new f-strings (formatted string literals):
在Python 3.6+中,您可以使用新的f-string(格式化字符串):
var_c = f"hello my name is: {var_a} and {var_b}, bye"
#2
7
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.
如前所述,您现有的输出是由于变量作为元组返回,而您希望它作为一个字符串返回。
#3
6
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使用tuple作为与*的参数
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)
#4
1
You should create var_c as a string, like this
您应该像这样创建var_c作为字符串。
var_c = "hello my name is: %s and %s bye" % (var_a, var_b)
#5
0
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创建为tuple…让它像绳子一样
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
#6
0
Your program is creating a tuple and you print the tuple:
您的程序正在创建一个tuple并打印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
#7
0
That's because you are using syntax to create a tuple!
这是因为您正在使用语法创建一个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"
#8
-1
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
#1
7
In Python 3.6+ you can use the new f-strings (formatted string literals):
在Python 3.6+中,您可以使用新的f-string(格式化字符串):
var_c = f"hello my name is: {var_a} and {var_b}, bye"
#2
7
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.
如前所述,您现有的输出是由于变量作为元组返回,而您希望它作为一个字符串返回。
#3
6
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使用tuple作为与*的参数
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)
#4
1
You should create var_c as a string, like this
您应该像这样创建var_c作为字符串。
var_c = "hello my name is: %s and %s bye" % (var_a, var_b)
#5
0
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创建为tuple…让它像绳子一样
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
#6
0
Your program is creating a tuple and you print the tuple:
您的程序正在创建一个tuple并打印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
#7
0
That's because you are using syntax to create a tuple!
这是因为您正在使用语法创建一个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"
#8
-1
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