在python中打印时如何添加字符串和列表?

2024-09-29 17:24:29 发布

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

user =input("please enter your user name\n")
passwd1=input("please enter your passwd\n")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('vlmanutslab1',username=user,password=passwd1)
stdin,stdout,stderr = ssh.exec_command(command1)
output1=stdout.readlines()
stdin,stdout,stderr = ssh.exec_command(command2)
output2=stdout.readlines()
stdin,stdout,stderr = ssh.exec_command(command3)
output3=stdout.readlines()
print( "vlmanutslab1" + " "+ output1 +" "  + " " + output2))

如何添加列表和字符串? 我得到的错误,如不能连接字符串和列表在一起。你知道吗


Tags: paramikoinputyourstderrstdinstdoutsshcommand
2条回答

你必须把它转换成字符串

一种方法是使用"".join()方法

print( "vlmanutslab1" + " "+" ".join(output1) +" "   + " ".join(output2))

要增加它们之间的空间,可以使用tab

print( "vlmanutslab1" + "\t\t\t\t"+" ".join(output1) +" \t\t\t\t"   + " ".join(output2))

您拥有的是一个字符串列表,您可以使用内置的^{} method.将一个字符串列表转换为一个字符串

这里很重要的一点是,您需要调用join()一个字符串,以便将字符串与字符串列表作为参数连接在一起。也就是说,字符串被添加到列表中的每一组连续字符串之间(而不是在开始和结束处)。你知道吗

如果要换行分隔:

print( "vlmanutslab1\n\n" + "\n".join(output1) + "\n\n"  + "\n".join(output2) )

尽管您可以用"\n"替换任何您喜欢的字符串。如果要在列表之间用两个空格分隔它们:

print( "vlmanutslab1  " + " ".join(output1) + "  "  + " ".join(output2) )

相关问题 更多 >

    热门问题