字符串的内置方法(较多,重要)
old_code = 'KeViN'
print('这是返回给用户的验证码: %s' % old_code)
new_code = input('请输入你的验证码:').strip()
print(new_code)
# 对验证码作一个判断,现在对验证码作不区分带小写
# if old_code.upper() == new_code.upper():
if old_code.lower() == new_code.lower():
print('验证码输入正确')
else:
print('验证码输入错误')
res = 'hello oldBoy'
# startswith, endswith
print(res.startswith('h')) # True
print(res.startswith('hel')) # True
print(res.startswith('hello1')) # False
print(res.endswith('y')) # True
print(res.endswith('Boy')) # True
print(res.endswith('boy')) # False
# 第一种方式:
# res = 'my name is {}, my age is {},{},{}'
# print(res.format('tank', 18)) # my name is tank, my age is 18
# 第二种方式:可以写索引,并且索引也可以反复使用
# res = 'my name is {0}, my age is {1},{0},{1}, {2}'
# print(res.format('kevin', 18, 'aa')) # my name is kevin, my age is 18,kevin,kevin
# 第三张方式:
# res = 'my name is {name1}, my age is {age1}, {name1}, {name1}, {age1}'
# print(res.format(name1='kevin', age1=18)) # my name is kevin, my age is 18
l = ['kevin', 'jack', 'tank', 'tony','kevin', 'jack', 'tank', 'tony','kevin', 'jack', 'tank', 'tony','kevin', 'jack', 'tank', 'tony']
# 把里面的每一个人名取出来,并以|作为连接符号,把所有人名连接起来
# kevin|jack|tank|tony
# print(l[0] + '|' + l[1] + '|' + l[2] + '|' + l[3]) # kevin|jack|tank|tony
# kevin|jack|tank|tony|kevin|jack|tank|tony|kevin|jack|tank|tony|kevin|jack|tank|tony
print('|'.join(l)) # kevin|jack|tank|tony
res = 'my name is jack jack jack jack'
print(res.replace('jack', 'kevin')) # my name is kevin kevin kevin kevin
print(res.replace('jack', 'kevin', 1)) # my name is kevin jack jack jack
print(res.replace('jack', 'kevin', 2)) # my name is kevin kevin jack jack
# 交换两个变量?
m = '10'
n = '20'
res = m + n # 1020
m = res.replace('10', '')
n = res.replace('20', '')
print(m, n)
guess = input('请输入你的年龄:').strip()
if guess.isdigit():
guess = int(guess)
else:
print('你什么玩意,能不能好好输入')
字符串了解的方法
# 如果找到了,就返回第一次在字符串中出现的位置索引
# print(msg.find('o')) # 1
# print(msg.find('s')) # 5
# print(msg.find('hello')) # 9
# 当找不到指定的字符串的时候,返回-1
# print(msg.find('hello1', 1, 5)) # -1
# print(msg.index('e')) # 10
# index:查找指定的字符串,如果找不到直接报错
print(msg.index('ee')) # 10
# 查找某个字符串出现的次数
print(msg.count('say'))
# 2.center,ljust,rjust,zfill
name = 'kevin'
# print(name.center(30, '*')) # ************kevin*************
# 左对齐
print(name.ljust(30, '*')) # kevin*************************
# 右对齐
print(name.rjust(30, '*')) # *************************kevin
# zfill
print(name.zfill(30)) # 0000000000000000000000000kevin
# # 4.capitalize,swapcase,title
message = 'hello everyone nice to meet you!'
print(message.capitalize()) # Hello everyone nice to meet you!
print(message.swapcase()) # HELLO EVERYONE NICE TO MEET YOU!
print(message.title()) # Hello Everyone Nice To Meet You!
print(message.islower())
print(message.isupper())
列表
# 类型转换
# 把字符串转为列表
print(list('hello')) # ['h', 'e', 'l', 'l', 'o']
print(list([1, 2, 3, 4])) # [1, 2, 3, 4]
print(list({'username':'kevin', 'age':18})) # ['username', 'age']
print(list((1, 2, 3, 4))) # [1, 2, 3, 4]
print(list({1, 2, 3, 4})) # [1, 2, 3, 4]
# list关键字可以把支持for循环的数据类型都转为列表
添加元素:
# 2. 添加元素
# 方式1:
# res=names_list.append('ly') # 它是在末尾追加元素
# print(res) # None:空
# print(names_list)
# names_list = ['kevin', 'tank', 'jack', 'tony', [11, 22, 33]]
# names_list = ['kevin', 'tank', 'jack', 'tony', 11, 22, 33]
# names_list.append([11, 22, 33]) # ['kevin', 'tank', 'jack', 'tony', [11, 22, 33]] 如果追加的是列表,会把列表整个元素都添加进去
# print(names_list)
names_list = ['kevin', 'tank', 'jack', 'tony']
# 方式2:
# names_list.insert(0, 666)
# names_list.insert(2, 999)
# names_list.insert(1, [1, 2, 3])
# print(names_list)
# 方式3:extend扩展元素,是用来做列表的合并的
# names_list.extend([1, 2, 3, 4]) # ['kevin', 'tank', 'jack', 'tony', 1, 2, 3, 4]
# print(names_list)
#
l = [1, 2, 3, 4]
for i in l:
names_list.append(i) # ['kevin', 'tank', 'jack', 'tony', 1, 2, 3, 4]
print(names_list)
# 删除元素
names_list = ['kevin', 'tank', 'jack', 'tony', 11, 22, 33]
# 方式1:
# del names_list[0] # del ------> delete
# del names_list[0] # del ------> delete
# del names_list[0] # del ------> delete
# print(names_list)
# 方式2:
# res=names_list.remove('kevin') # 直接写元素值,而不是索引
# print(res) # None
# print(names_list)
# 方式3:弹出元素
# res=names_list.pop() # ['kevin', 'tank', 'jack', 'tony', 11, 22]
# res=names_list.pop(1) # ['kevin', 'tank', 'jack', 'tony', 11, 22]
# print(res) # 33
# print(res) # tank
# print(names_list)
可变类型与不可变类型
res = 'helloworld'
print(id(res))
res1 = res.title()
print(id(res1))
print(res.title()) # Helloworld
print(res) # helloworld
name_list = ['kevin', 'tank', 'jack']
print(id(name_list))
name_list.append('tony')
print(id(name_list))
print(name_list) # ['kevin', 'tank', 'jack', 'tony']
# 可变类型:列表、字典
# 改变的是原来的值, 它的内存地址没有改变,
# 不可变类型:字符串、整型、浮点型
# 不改变原值,它改变内存地址
列表需要了解的方法
# 8.sort()给列表内所有元素排序
# lst = [9, 8, 7, 1, 2, 3, 4, 5, 6]
# s = ['kevin', 'tank', 'jack', 'xyz']
# lst.sort() # [1, 2, 3, 4, 5, 6, 7, 8, 9]
# lst.sort(reverse=True) # [9, 8, 7, 6, 5, 4, 3, 2, 1]
# s.sort() # ['jack', 'kevin', 'tank', 'xyz']
# print(s)
# print(lst) #
l1 = [1, 2, 3]
l2 = [1, 2, ]
print(l1 > l2) # False
队列和栈的概念
# 队列和栈的概念是在数据结构里面存在的
链表、单链表、双链表、循环链表、栈、队列、树、二叉树、平衡二叉树、红黑树、b树、b+树、b-树、图
# 队列:先进先出
# 栈:先进后出
标签:内置,tank,res,list,元组,print,jack,类型,kevin
From: https://www.cnblogs.com/huangchunfang/p/17560375.html