小甲鱼的嫖客 发表于 2020-7-2 10:43:47

print 自动加入空格怎么弄,每日一问。

like_numbers={'ton':'66','jerry':'88','nike':'250','nick':'1230','deha':'100'}
for man in like_numbers:
   print( man+(" ") +"like"+(" ")+like_numbers + (" ") + "very much !")
我加入了(" ")才能让每次词之间隔开,\t我没用明白,求教大佬们。

tonlike66very much !
jerrylike88very much !
nikelike250very much !
nicklike1230very much !
dehalike100very much !

ton like 66 very much !
jerry like 88 very much !
nike like 250 very much !
nick like 1230 very much !
deha like 100 very much !

永恒的蓝色梦想 发表于 2020-7-2 10:47:17

like_numbers={'ton':'66','jerry':'88','nike':'250','nick':'1230','deha':'100'}
for man in like_numbers:
   print(man, "like", like_numbers, "very much !")

小甲鱼的嫖客 发表于 2020-7-2 10:55:25

永恒的蓝色梦想 发表于 2020-7-2 10:47


我复制了你的代码运行失败了。

永恒的蓝色梦想 发表于 2020-7-2 11:01:50

小甲鱼的嫖客 发表于 2020-7-2 10:55
我复制了你的代码运行失败了。

然而我运行成功了。报错是什么?

yhhpf 发表于 2020-7-2 11:02:10

本帖最后由 yhhpf 于 2020-7-2 11:04 编辑

>>> like_numbers={'ton':'66','jerry':'88','nike':'250','nick':'1230','deha':'100'}
>>> for man in like_numbers:
   print( man+("\t") +"like"+("\t")+like_numbers + ("\t") + "very much !")

   
ton        like        66        very much !
jerry        like        88        very much !
nike        like        250        very much !
nick        like        1230        very much !
deha        like        100        very much !
\t 相当于TAB了一下~

Twilight6 发表于 2020-7-2 11:13:17

这样可以不? join 函数进行拼接, join 前面是要拼接间隔的元素,这下面我间隔了 一个空格:
like_numbers={'ton':'66','jerry':'88','nike':'250','nick':'1230','deha':'100'}
for man in like_numbers:
   temp = ''.join(,"very much !"])
   print(temp)

运行结果:
ton66very much !
jerry88very much !
nike250very much !
nick1230very much !
deha100very much !

Twilight6 发表于 2020-7-2 11:20:19

小甲鱼的嫖客 发表于 2020-7-2 10:55
我复制了你的代码运行失败了。

2L代码没问题,是你复制错了吧,我的方法还麻烦了点

wingl 发表于 2020-7-2 15:56:51

print里面用逗号隔开会默认带空格的,需要的话可以用sep参数更改

https://fishc.com.cn/forum.php?mod=viewthread&tid=141463&extra=page%3D1%26filter%3Dtypeid%26typeid%3D768
页: [1]
查看完整版本: print 自动加入空格怎么弄,每日一问。