对象
对象:数字,字符串(所有的数据都是对象)
使用id()函数和type()函数获取对象信息
n = 3
id(n)
执行程序时,python会自动为对象的id赋一个独特的整数;
在程序的执行过程中,对象的id不会改变;
每当执行程序时,python都可能会赋一个不同的id.
f = 3.0
type(f)
在python中,类和类型是一样的意思;
一个对象的类型由类决定:
- 字符串的类是str
- 整数的类是int
- 浮点数的类是float
对对象执行操作
s = "Welcome"
s1 = s.lower() # 'welcome'
s2 = s.upper() # 'WELCOME'
s = "\t Welcome \n"
s1 = s.strip() # 'Welcome'
# 去除空格符:' '. \t, \n, \f, \r
格式化 format函数
print(format(57.467890, "10.2f"))
print(format(57, ".2f"))
print(format(57.467657, "10.2e"))
# □□5.75e+01 符号'+'和'-'被算入宽度
print(format(0.53457, "10.2%"))
# □□□□53.46% 符号%算入宽度
print(format(57.467657, "<10.2f"))
# 忽略空格,左对齐
# 57.47 (前面无空格)
print(format(59832, "10d"))
print(format(59832, "10x"))
print(format(59832, "10o"))
print(format(59832, "10b"))
# 10宽度,转换为10,16,8,2进制
print(format("Welcome to ...", "20s"))
print(format("Welcome to ...", "<20s")) # 左对齐
print(format("Welcome to ...", ">20s")) # 右对齐