admin管理员组

文章数量:1589747

python -- 用户交互interaction

input() 和format()用法

name = input("User Name:\n")
password = input("User Password:\n")
age = int(input("User Age:\n"))
# 建议使用format这样,取代%的用法
user_info = '''
Name:{0}
Password:{1}
Age:{2}'''.format(name,password,age)
print(user_info)

getpass方法

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

import getpass
name = input("User Name:\n")
# 在Pycharm里面不能使用,在cmd里可以
password = getpass.getpass("User Password:\n") 
写在后面
  • 使用type(),表示数据类型
a = "Gao"
# 这里就是用来调用type()函数
b = type(a)  
print(b)
  • 使用int(),将字符串转换成整型
a = "13"
# a一定要是一个数字形式的字符串,不然会报错
b = int(a)  
print(b)
  • 使用str(),转换成字符串
a = 13
b = str(a)
c = ['name',11,'age',{11:22}]
d = str(c)
print(b,d)
  • 字符串拼接,建议使用format格式,使用format是在内存里开辟一个内存空间,而使用加号+做字符串拼接,却是在内存里开辟多个内存空间,效率==低下==。

转载于:https://wwwblogs/gzz041/p/7061836.html

本文标签: 用户PythonInteraction