字符串:
1,格式化字符串:
a = 'hello'
#字符串可以进行加法运算
#字符串只能和字符串拼接,如果出现其他类型进行加法运算,会报错。
a = "hello" + "world"
print(a)
print("a = ", a)
print('a')
#在创建的字符串中,可以在字符串中指定占位符。
#%s 在字符串中表示任意字符串
#%f 浮点占位符
#%d 整数占位符
b = "hello %s" % 'world'
print(b)
b = "hello %s, %s" % ('world', 'tom')
print(b)
b = "hello %3s, %s" % ('world', 'tom') #%3s 取3个字符
print(b)
b = "hello %3.5s, %s" % 'ab' #%3.5 字符串限制在3-5之间。
>>> helloab
b = 'hello %f' % 123.45 #%f 浮点占位符
#格式化字符串
#在格式化字符串中可以直接嵌入变量
c = f'hello {a} {b}'
print(f'c = {a}')
四种方式:
name = 'tom'
print('hello' + name)
print('hello', name)
print('hello %s' % name)
print(f'name {name}')
2, 复制字符串
#字符串中和数字相乘,则将字符串重复复制。
a = 'abc'
a = a * 2
3, 布尔值和空值
#bool True False
#布尔值也属于整型,True相当于1,False相当于0
print(1 + False)
#None 专门表示不存在。
b = None
4,类型检查
#通过类型检查,可以检查变量的类型
a = 123 #a是整型
b = '123' #b是字符串
#type() 检查值得类型
c = type(a)
print(c)