利用百分号格式化:
s1 = "I love python %d 遍" print(s1%(1000)) s1 = "I love python %d 遍 and %d" print(s1%(100, 1000))
format函数格式化字符串:
# format例子 s1 = "I love python {} 遍" print(s1.format(100) s1 = "I love python {} 遍 and {} 遍" print(s1.format(100, 1000) s1 = "I love python {0} 遍 and {0} 遍 and {1} 遍 and {1} 遍" print(s1.format(100, 1000)
# 数字类型 (整型、浮点类型、布尔类型) varn = 100 varn2 = 510.1 print(varn, type(varn)) print(varn2, type(varn2))
# 字符串类型 a = '你好' res = type(a) print(a, res) # 大字符串 s = """ aaaaa bbbbb ccccc """ # 字符串中的引号可以相互嵌套,但不能嵌套自己 s1 = """ aaa'a'a bbb"bb" cc'''ccc'''c """ # 转义字符 s = 'abc\ndef' print(s) s = r'abc\ndef' print(s)
# 集合的定义 s = set() print(s) s = {3, 19, 91, 3, 9, 19, 10} print(s) # 集合的去重 li = [2, 3, 1, 2, 3, 22, 11] s = set(li) print(s) # 添加内容 删除内容 s.add(300) print(s) s.remove(300) print(s) # 交差并补 s1 = {1, 2, 3, 4} s2 = {3, 4, 5, 6, 7} s3 = s1.intersection(s2) s3 = s1.difference(s2) s3 = s1.union(s2) s3 = s1.symmetric_difference(s2)
三大结构: