python入门基础

黑猴子的家:Python 用户输入

2019-01-10  本文已影响11人  黑猴子的家

1、普通输入

code

#!/usr/bin/env python
#_*_coding:utf-8_*_
 
 
#name = raw_input("What is your name?") #only on python 2.x
name = input("What is your name?")
print("Hello " + name )

运行

What is your name?  hei hou zi de jia
Hello hei hou zi de jia

2、隐藏输入的密码

输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法

code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
  
import getpass
  
# 将用户输入的内容赋值给 name 变量
pwd = getpass.getpass("请输入密码:")
  
# 打印输入的内容
# getpass 只能在cmd python 交互式 里面演示
print(pwd)

运行

请输入密码:******
123456

3、格式化输出

code

# Author:黑猴子

name = input("name:")
age = int(input("age:") ) #integer
print(type(age),type(str(age)))
job = input("job:")
salary  = input("salary:")

info0 = '''
-------- info0 of  ''' + name + '''  -----
Name: ''' + name

print("info0",info0)

info1 = '''
-------- info1 of  %s  -----
Name:%s
Age:%d
Job:%s
Salary:%s
''' % (name,name,age,job,salary)

print("info1",info1)

info2 = '''
-------- info2 of {_name}  -----
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
'''.format(_name=name,
           _age=age,
           _job=job,
           _salary=salary)

print("info2",info2)

info3 =  '''
-------- info3 of {0} -----
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
'''.format(name,age,job,salary)

print("info3",info3)

运行

name:hei hou zi de jia
age:18
<class 'int'> <class 'str'>
job:yuan ding
salary:9999999

info0 
-------- info0 of  hei hou zi de jia  -----
Name: hei hou zi de jia

info1 
-------- info1 of  hei hou zi de jia  -----
Name:hei hou zi de jia
Age:18
Job:yuan ding
Salary:9999999

info2 
-------- info2 of hei hou zi de jia  -----
Name:hei hou zi de jia
Age:18
Job:yuan ding
Salary:9999999

info3 
-------- info3 of hei hou zi de jia -----
Name:hei hou zi de jia
Age:18
Job:yuan ding
Salary:9999999
上一篇下一篇

猜你喜欢

热点阅读