Python:字符串中引用外部变量的3种方法

方法一:

 1 username=input('username:')
 2 age=input('age:')
 3 job=input('job:')
 4 salary=input('salary')
 5 
 6 info1='''
 7 -----------------------info of %s----------------------------
 8 Name:%s
 9 Age:%s
10 Job:%s
11 Salary:%s
12 ''' %(username,username,age,job,salary)
13 
14 print(info1)

方法二:

 1 username=input('username:')
 2 age=input('age:')
 3 job=input('job:')
 4 salary=input('salary:')
 5 
 6 info2='''
 7 ---------------------------------info of {_name}------------------
 8 Name:(_name)
 9 Age:{_age}
10 Job:{_job}
11 Salary:{_salary}
12 '''.format(_name=username,
13               _age=age,
14               _job=job,
15               _salary=salary)
16 
17 print(info2)

方法三:

 1 username=input('username:')
 2 age=input('age:')
 3 job=input('job:')
 4 salary=input('salary:')
 5 
 6 info3='''
 7 ---------------------------------info of {0}---------------------
 8 Name:{0}
 9 Age:{1}
10 Job:{2}
11 Salary:{3}
12 '''.format(username,age,job,salary)
13 
14 print(info3)

 

转载于:https://www.cnblogs.com/duwenxing/p/7347637.html

你可能感兴趣的:(Python:字符串中引用外部变量的3种方法)