想改变输出方式应该怎么改写?
# coding=utf-8
class Animal(object): pass
dog=Animal()
cat=Animal()
dog.name='汪汪'
dog.age=7
cat.name='喵喵'
cat.age=3
print(dog.name)
print(dog.age)
print(cat.name)
print(cat.age)
--->我想把我的现在的这个输出结果改成 汪汪:7 喵喵:3这种,应该怎么写print吖
# coding=utf-8
class Animal(object): pass
dog=Animal()
cat=Animal()
dog.name='汪汪'
dog.age=7
cat.name='喵喵'
cat.age=3
print(dog.name)
print(dog.age)
print(cat.name)
print(cat.age)
--->我想把我的现在的这个输出结果改成 汪汪:7 喵喵:3这种,应该怎么写print吖
2024-06-11
# Enter a code
# coding=utf-8
class Animal(object):
pass
dog = Animal()
dog.name = '汪汪'
dog.age = 7
# 三种方式都可以,最后一种3.6新功能,在网页上还不支持,可以本地执行
print("%s : %s" % (dog.name, dog.age))
print("{} : {}".format(dog.name, dog.age))
print(f"{dog.name}: {dog.age}")
cat = Animal()
cat.name = '喵喵'
cat.age = 3
print("%s : %s" % (cat.name, cat.age))
print("{} : {}".format(cat.name, cat.age))
print(f"{cat.name}: {cat.age}")
举报