Python新手结合文本

时间:2013-06-08 14:57:02

标签: python

如何在等号后删除空格?我搜索了整个谷歌,找不到任何关于如何做到这一点。非常感谢任何帮助。

Code
    customer = input('Customer Name:')
    mpid = input('MPID=<XXXX>:')
    print ('description' ,customer,'<MPID=',mpid+'>')

Output
    Customer Name:testcustomer

    MPID=<XXXX>:1234

    description testcustomer <MPID= 1234>

4 个答案:

答案 0 :(得分:3)

以下是一些组合字符串的方法......

name = "Joel"
print('hello ' + name)
print('hello {0}'.format(name))

所以你可以在你的情况下使用其中任何一个......

print('description', customer, '<MPID={0}>'.format(mpid))
print('description {0} <MPID={1}>'.format(customer, mpid))

答案 1 :(得分:1)

print ('description',customer,'MPID='+str(mpid)+'>')

我认为这就是你要做的。你已经用关闭尖括号进行了无空间连接。

答案 2 :(得分:0)

由于标题是一般的,我认为这可能对某些人有帮助,即使它与完整的问题没有直接关系:

当解释器在同一行或续行上彼此相邻时,它会将文本字符串(但不是字符串重新组合)组合在一起。

>>>'this sen'   "tence w"                  '''ill be combined'''
'this sentence will be combined'

这允许在长字符串中使用换行符和空格来提高可读性,而不会让程序必须处理它们的重组。

>>>('1
     2
     3
     4')
'1234'

答案 3 :(得分:0)

print(a, b, c)a放入输出流,然后是空格,然后是b,然后是空格,然后是c

要避免空间,请创建一个新字符串并将其打印出来。你可以:

连接字符串:a + b + c

更好:加入字符串:''.join(a, b, c)

更好:格式化字符串:'description {0} <MPID={1}>'.format(customer, mpid)

相关问题