为什么我的弦用花括号括起来?Python

2024-10-02 02:36:38 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我正在开发的tikinter应用程序的代码片段

stringGreet = "Hello Employee"+myresults[0],myresults[1]+"who is"+myresults[2]
managerGreeting.set(stringGreet)
raiseFrame(managerMenuFrame)

为什么这段代码会导致串接的字符串被大括号环绕? 结果不正确

result

结果正确

Hello Employee Joe Smalls who is Male

Tags: 字符串代码应用程序helloisemployee大括号who
2条回答

您将元组输入到managerGreeting.set(stringGreet),因为您创建了一个元组:

stringGreet = "Hello Employee"+myresults[0]  ,  myresults[1]+"who is"+myresults[2]

创建单个字符串:

stringGreet = "Hello Employee {},{} who is {}".format(myresults[0],myresults[1],myresults[3])

你也可以使用

stringGreet = "Hello Employee {},{} who is {}".format(*myresults[:3]) 

相反。如果myresults只包含3个元素,请删除[:3]

如果逗号不正确,请考虑使用f-string

stringGreet = f"Hello Employee {myresults[0]} {myresults[1]} who is {myresults[2]}"

相关问题 更多 >

    热门问题