# 1.字符串的索引取值: 字符串[index]
# 正向取值从0编号,反向取值从-1编号
s1 = '123abc呵呵'
print(id(s1)) # 2217572683576
print(s1[0], id(s1[0])) # 2217543167200
t_s = '1'
print(id(t_s)) # 2217543167200
# 取出c
print(s1[5], s1[-3])
# 2.字符串拼接
s2 = '你好'
s22 = '帅'
ss2 = s2 + s22
print(ss2, id(s2), id(s22), id(ss2))
# 如果要拼接其他类型
a = 10
b = "20"
c = True
# res = "1020True"
res = "%s%s%s" % (a, b, c) # 方法1
print(res)
res = str(a) + b + str(c) # 方法2
print(res)
# 3.字符串长度
s3 = '12345'
ln1 = s3.__len__()
print(ln1)
ln2 = len(s3)
print(ln2)
# 4.字符串切片: 取子字符串 - [::] - [start_index:end_index:step]
s4 = '123abc呵呵'
sub_s = s4[0:6:]
print(sub_s) # 123abc
sub_s = s4[0:6:2]
print(sub_s) # 13b
sub_s = s4[::-1]
print(sub_s) # 呵呵cba321
sub_s = s4[-1:-6:-1]
print(sub_s) # 呵呵cba
# 5.成员运算:判断某字符串是否在该字符串中
s5 = '123abc呵呵'
ss5 = '12a'
print(ss5 in s5) # False
print(ss5 not in s5) # True
# 6.字符串循环(遍历)
s6 = '123abc呵呵'
for v in s5:
print(v)
# 7.索引(目标字符串的索引位置)
s1 = '123abc呵呵'
print(s1.index('b'))
# 8.去留白(默认去两端留白,也可以去指定字符)
s2 = '***好 * 的 ***'
print(s2.strip('*'))
# 9.计算子字符串个数
s3 = '12312312'
print(s3.count('123'))
# 10.判断字符串是否是数字:只能判断正整数
s4 = '123'
print(s4.isdigit())
# 11.大小写转换
s5 = "AbC def"
print(s5.upper()) # 全大写
print(s5.lower()) # 全小写
# 了了解
print(s5.capitalize()) # 首字母大写
print(s5.title()) # 每个单词首字母大写
# 12.以某某开头或结尾
s6 = 'https://www.baidu.com'
r1 = s6.startswith('https:')
r2 = s6.startswith('http:')
r3 = s6.endswith('com')
r4 = s6.endswith('cn')
if (r1 or r2) and (r3 or r4):
print('合法的链接')
else:
print('非合法的链接')
# 13.替换
s7 = 'egon say: he is da shuai b,egon!egon!egon!'
new_s7 = s7.replace('egon', 'Liu某') # 默认替换所有
print(new_s7)
new_s7 = s7.replace('egon', 'Liu某', 1) # 替换一次
print(new_s7)
# 14.格式化
s8 = 'name:{},age:{}'
print(s8.format('Owen', 18)) # 默认按位置
print('name:{1},age:{1}, height:{1}'.format('Owen', 18)) # 标注位置,一个值可以多次利用
print('name:{n},age:{a}, height:{a}'.format(a=18, n="Zero")) # 指名道姓
# 1.索引取值: 列表名[index]
s1 = [1, 3, 2]
print(s1[0])
print(s1[-1])
# 2.列表运算: 得到的是新list
s2 = [1, 2, 3]
print(s2 + s2)
print(s2 * 2)
print(s2)
# 3.list的长度
s3 = [3, 4, 1, 2, 5]
print(len(s3))
# 4.切片:[start_index:end_index:step]
s4 = [3, 4, 1, 2, 5]
new_s4 = s4[::-1]
print(new_s4)
new_s4 = s4[1:4:]
print(new_s4)
new_s4 = s4[-2:-5:-1]
print(new_s4)
# 5.成员运算:in
s5 = [3, 4, '1', 2, 5]
print('1' in s5)
print(1 in s5)
print(5 not in s5)
# 6.循环
for v in s5:
print(v, type(v))
# 只打印数字类型的数据
for v in s5:
if isinstance(v, int):
print(v, end=' ')
# 7.列表的增删改查
ls = [1, 2, 3]
# 查
print(ls)
print(ls[1])
# 增
ls.append(0) # 末尾增
print(ls)
ls.insert(1, 666) # 任意index前增
print(ls)
ls.insert(len(ls), 888) # insert实行末尾增
print(ls)
# 改
ls[1] = 66666
print(ls)
# 删
ls.remove(888)
print(ls)
res = ls.pop() # 默认从末尾删,并返还删除的值
print(res)
res = ls.pop(1) # 从指定索引删除,并返还删除的值
print(res, ls)
# 了了解
del ls[2]
print(res, ls)
# 清空
ls.clear()
print(ls)