Skip to main content
 首页 » 编程设计

python之如何打印没有空格的文本行

2025年05月04日12飞鱼

我正在尝试从用户那里获取两个值并根据用户输入打印一行文本。

char1 = input("Please enter the first character: ") 
char2 = input("Please enter the secound character: ") 
 
width = int(input("Please enter the width of the innermost triangle: ")) 
 
if width % 2 == 0: 
    print ("ERROR - number is even") 
    quit() 
 
print ("") 
print (char1*11,char2*1,char1*11) 

这是我当前的输出:

YYYYYYYYYYY + YYYYYYYYYYY 

我正在尝试不带空格地打印它,以便它看起来像这样:

YYYYYYYYYYY+YYYYYYYYYYY 

我该怎么做?

请您参考如下方法:

像这样打印你的输出:

print ("{}{}{}".format(char1*11,char2*1,char1*11)) 

或者如果您使用的是 Python 3,则可以使用 sep:

print (char1*11,char2*1,char1*11, sep="") 

在这里阅读更多关于打印的信息:

http://www.python-course.eu/python3_print.php