python数据类型转换&&格式化输出
①python的数据类型包含:数字、字符串、列表、元组、字典、集合这六种基本数据类型。不同数据类型的数据可以进行类型的转换。
使用input让用户输入的数据默认为字符串类型;
1 name = input("name:") 2 age = input("age:") 3 print(type(name)) 4 print(type(age), type(int(age))) 5 6 >>>name:xiaoming 7 >>>age:20 8 <class 'str'> 9 <class 'str'> <class 'int'>
②格式化输出主要使用以下三种方式:
1 name = input("name:") 2 age = int(input("age:")) 3 job = input("job:") 4 salary = input("salary:")
方式一、
1 info = ''' 2 -------- info of %s ----- #%s,%d 为占位符 3 Name:%s 4 Age:%d 5 Job:%s 6 Salary:%s 7 ''' % (name,name,age,job,salary)
8 print(info)
>>>name:xiaoming
>>>age:20
>>>job:python developer
>>>salary:10000
-------- info of xiaoming -----
Name:xiaoming
Age:20
Job:python developer
Salary:10000
方式二、
1 info2 = ''' 2 -------- info of {_name} ----- 3 Name:{_name} 4 Age:{_age} 5 Job:{_job} 6 Salary:{_salary} 7 '''.format(_name=name, 8 _age=age, 9 _job=job, 10 _salary=salary)
>>>name:xiaoming
>>>age:20
>>>job:python developer
>>>salary:10000
-------- info of xiaoming -----
Name:xiaoming
Age:20
Job:python developer
Salary:10000
方式三、
1 info3 = ''' 2 -------- info of {0} ----- 3 Name:{0} 4 Age:{1} 5 Job:{2} 6 Salary:{3} 7 '''.format(name,age,job,salary)
>>>name:xiaoming
>>>age:20
>>>job:python developer
>>>salary:10000
-------- info of xiaoming -----
Name:xiaoming
Age:20
Job:python developer
Salary:10000