.滚筒洗衣机. 发表于 2021-10-16 06:36:36

Python萌新求大佬帮忙

        import random
        加数1 = random.randint(1,10)
        加数2 = random.randint(1,10)
   
        print(加数1"+"加数)
   我想同时一行打印变量和字符串,我该怎么办?
   

.滚筒洗衣机. 发表于 2021-10-16 06:37:23

求大佬帮助

大马强 发表于 2021-10-16 07:32:32

变量和字符串之间用, 隔开就好
import random
加数1 = random.randint(1, 10)
加数2 = random.randint(1, 10)
print(加数1, "+", 加数2, "=", 加数1+加数2, sep="")

suchocolate 发表于 2021-10-16 08:52:00

a, b, c = '1', 2,
print(a,b,c)   # 逗号分隔对象方式
print(f'{a} {b} {c}')# f字符串带入变量方式
print('{} {} {}'.format(a,b,c))# format方式
print('%s %s %s' % (a,b,c))# 老版本字符串格式化方式

Python初学者8号 发表于 2021-10-16 09:47:26

楼上提供了正解了,说白了还是你对print函数的使用没有掌握
页: [1]
查看完整版本: Python萌新求大佬帮忙