简体   繁体   中英

printing special characters without spaces

I made a for loop to write a little program that has an if condition. I want to print a number and put it between '' without any space. Here is a portion of my program :

for y in range(2):  

print('  if a ==','\'',y,'\'' ,'and',' b ==','\'',y,'\'',':')

My output is

  • if a == ' 0 ' and b == ' 0 ' :

    if a == ' 1 ' and b == ' 1 ' :

but I want it to be :

  • if a == '0' and b == '0' :

    if a == '1' and b == '1' :

without any spaces between the ' and the number.

Elaborating on my comment. You can use + instead. However a much better way of "formatting" you print statements exactly the way you want is to use .format .

print ('If a == '{0}' and b == '{0}'.format(str(y)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM