Study 2 —— 格式化输出
Posted yancy.lu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Study 2 —— 格式化输出相关的知识,希望对你有一定的参考价值。
打印人物信息的两种方法
第一种:
Name = input(‘Input your name: ‘) Age = input(‘Input your age: ‘) Job = input(‘Input your job: ‘) Hometown = input(‘Input your hometown: ‘) print(‘---------- Info of‘, Name,‘----------‘) print(‘Name: ‘, Name) print(‘Age: ‘, Age) print(‘Job: ‘, Job) print(‘Hometown: ‘, Hometown) print(‘------------- End -------------‘)
第二种:
Name = input(‘Input your name: ‘) Age = input(‘Input your age: ‘) Job = input(‘Input your job: ‘) Hometown = input(‘Input your hometown: ‘) info = ‘‘‘ ---------- Info of %s ---------- Name: %s Age : %s Job : %s Hometown: %s -------------- End ------------- ‘‘‘ % (Name, Name, Age, Job, Hometown)
print(info)
两种方法实现的结果都如下所示:
C:\Users\Administrator\Desktop>python information.py
Input your name: Lisa
Input your age: 18
Input your job: modal
Input your hometown: UK
---------- Info of Lisa ----------
Name: Lisa
Age : 18
Job : modal
Hometown: UK
-------------- End -------------
注:第二种方法是将人物信息进行格式化输入输出,更加方便。%s是占位符
%s 字符串(string)
%d 数字(digit)
%f 小数(float)
所以上面的代码还可以这样写:
Name = input(‘Input your name: ‘)
Age = int(input(‘Input your age: ‘))
Job = input(‘Input your job: ‘)
Hometown = input(‘Input your hometown: ‘)
info = ‘‘‘
---------- Info of %s ----------
Name: %s
Age : %d
Job : %s
Hometown: %s
-------------- End -------------
‘‘‘ % (Name, Name, Age, Job, Hometown)
print(info)
以上是关于Study 2 —— 格式化输出的主要内容,如果未能解决你的问题,请参考以下文章