首页 > 编程语言 >python day07 字符串和列表的数据内置方法

python day07 字符串和列表的数据内置方法

时间:2023-05-19 20:25:59浏览次数:45  
标签:内置 python day07 list names res print kevin jack

字符串的内置方法(较多,重要)

lower(字符串全部转为小写)

upper(字符串全部转为大写)

# 应用:验证码不区分大小写
old_code = 'KeVin' print('这是返回给用户的验证码%s' % old_code) new_code = input('请输入你的验证码:').strip() print(new_code) if new_code.lower() == old_code.lower(): print('验证码输入正确') else: print('验证码输入错误') print(old_code.lower()) print(old_code.upper())

startswith(字符串以什么开始)

endswith(字符串以什么结束)

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

格式化输出之 format

# 第一种方式
res = 'my name is {},age is {}, and my hobby is {}' print(res.format('tom', '18', 'spa')) # 第二种方式
res = 'my name is {name},age is {age}, and my hobby is {hobby}.' print(res.format(name='tom', age=18, hobby='spa')) # 第三种方式:可以写索引,并且索引也可以反复使用
res = 'my name is {0},age is {1}, and my hobby is {2}' print(res.format('tom', 18, 'spa'))

join

l = ['kevin', 'jack', 'tank', 'tony', 'kevin', ]
print('|'.join(l))
# kevin|jack|tank|tony|kevin

replace(替换)

res = 'my name is tom and i like spa.my friend tom like with me.'
print(res.replace('tom', 'jack'))
print(res.replace('tom', 'lili', 2))# 替换几个
# 交换变量

m = '10'
n = '20'
res = m + n # 1020
m = res.replace('10', '')
n = res.replace('20', '')
print(m, n)

isdigit(判断是不是纯整数)

guess = input('请输入你的年龄:').strip()
if guess.isdigit():
    guess = int(guess)
else:
    print('你什么玩意,能不能好好输入')

字符串了解的方法

 res = 'hello world'
# # 查看某个字符在索引的哪个位置
print(res.find('l'))  # 2
print(res.find('d'))  # 10
print(res.find('o', 5, 9))  # 在规定的范围内寻找字符在索引的哪个位置
print(res.index('1'))  # index:查找指定的字符串,如果找不到直接报错
res = 'hello world'
print(res.count('l'))  # 某个字符出现的次数
# 2.center,ljust,rjust,zfill
res = 'hello world'
print(res.center(15, '@'))  # 在字符串量变补充,默认空格。
print(res.ljust(20, '$'))  # 左对齐,剩余位数默认以空格补齐
print(res.rjust(20, '$'))  # 右对齐,剩余位数默认以空格补齐
print(res.zfill(20))  # 在字符串前面用0补充位数
# 4.capitalize,swapcase,title
message = 'hello everyone nice to meet you!'
print(message.capitalize())  # 字符串句子首字母大写
print(message.swapcase())  # 字符串句子全部大写
print(message.title())  # 字符串单词首字母全部大写
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]

添加元素

方式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)

可变类型和不可变类型

可变类型:列表、字典
改变的是原来的值, 它的内存地址没有改变,
不可变类型:字符串、整型、浮点型
不改变原值,它改变内存地址

 

列表需要了解的方法

# 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)

 

队列和栈的概念

队列和栈的概念是在数据结构里面存在的

数据结构:链表、单链表、双链表、循环链表、栈、队列、树、二叉树、平衡二叉树、红黑树、b树、b+树、b-树、图

队列:先进先出
栈:先进后出

 

标签:内置,python,day07,list,names,res,print,kevin,jack
From: https://www.cnblogs.com/zfq132/p/17416181.html

相关文章

  • 【python】【报错:pip中第三库下载成功,但是pycharm却没有显示】一步解决
     解决方案: 直接在这个目录下安装第三方库 ......
  • python基础07
    字符串的内置方法(较多,重要)lower&upperold_code='KeViN'print('验证码:%s'%old_code)s=input('请输入验证码:>>>').strip()ifold_code.lower()==s.lower():#lower改为全小写print('验证成功')elifold_code.upper()==s.up......
  • win下python2与python3共存
    1.先下载py2和py3 2.添加环境变量   3.在Path(系统变量)添加值(中间要分号隔开): 1.c:\Python272.c:\Python27\Scripts3.C:\Users\Administrator\AppData\Local\Programs\Python\Python364.C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts少哪个加哪个,注......
  • python批量处理excel数据、批量透视、合并透视表
    ###t+21importpandasaspdimportos#半自动———#——<创建好格式文件夹#——<数据下载至文件夹#——<修改date#——<运行##直播时间date='4.21'#读取存放文件路径read_path='E:/test/t+21/'+date+'/1原始数据'path_list=os.listdir(read_path)pathdir=''writ......
  • 使用国内镜像通过pip安装python 包
    pip安装python包时超时或是报错“CannotfetchindexbaseURLhttp://e.pypi.python.org/simple/”解决方法:加上参数"-i http://pypi.v2ex.com/simple",即换成国内镜像。一劳永逸的办法:~/.pip/下创建文件pip.conf(如果还没有的话),并填入以下内容:1[global]2timeout=6......
  • Python 列表append自己
    list=['zz','ss']list.append(list)print(id(list[2]))print(id(list[2][0]))print("1:",list)list.append(list)print(id(list[3]))print(id(list[3][0]))print("2:",list)结果:1404637464558161404637464107361:['z......
  • python使用exchangelib读取、保存exchange邮件
    importosfromdatetimeimportdatetimeimportpytzfromexchangelibimportCredentials,Account,Configuration,DELEGATE,Q,FileAttachmentdefreceived_exchange_message():"""接收exchange邮件,保存邮件到本地:return:""......
  • centos 8 安装python3
    sudodnfinstallpython3  查看版本 查看pip ......
  • 01_Python 基础
    01_Python基础Python解释器&pycharm安装注释#变量常量input#输入框if#条件判断while#循环关键字continuebreak关键字pass01.for循环for变量in可迭代的东西:代码把可迭代的东西中的每一项内容拿出来,挨个的赋值给变量,每一次赋值都要执行......
  • python os.environ操作环境变量
    1、.env文件数据添加到环境变量load_dotenv(ROOT_DIR+'/.env',verbose=True)或load_dotenv(find_dotenv('.env')2、新增环境变量,其中key和value均为string类型os.environ['环境变量名称']='环境变量值'  环境变量立刻生效,从设置开始后面的运行部分都能获取到该变量os.e......