python3-交互与字符串格式化

2023-01-31 01:01:40 python3

# Auther: Aaron Fan

#字符转格式化:

name = input("Enter your name: ")
age = int(input("Enter your age: "))
job = input("Enter your job: ")

#方法1(最推荐的方法)
info2 = '''
   ---------- The %s info ----------
   Name is %s.
   Age is %d.
   Job is %s
''' % (name,name,age,job)
print(info2)

#方法2(知道有这个东西就行了)
info2 = "---------- The %s info ----------"
info2 += "\nName is " + name
info2 += "\nAge is " + str(age)
info2 += "\nJob is " + job
print(info2)

#方法3(这种写法必须按顺序来,知道有这个东西就行了)
info2 = '''
   ---------- The {0} info ----------
   Name is {1}
   Age is {2}
   Job is {3}
''' .fORMat(name,name,age,job)
#day2中的str字符串操作中有介绍另一种format的用法,可以不用按照顺序去写
print(info2)


相关文章