python日常练习二 字符串 ,列表
练习一 String1. 给定一个字符串str = “helloworld”,利用所学字符串的切片知识,反转字符串;str ="helloworld"print(str[::-1])2. 给定一个字符串str = “my name is baoabo”,将“ ”(空格)替换为“,”,并输出显示;str = "my name is baoabo"str1 = str.replace(' ', ','
·
练习一 String
1. 给定一个字符串str = “helloworld”,利用所学字符串的切片知识,反转字符串;
str ="helloworld"
print(str[::-1])
2. 给定一个字符串str = “my name is baoabo”,将“ ”(空格)替换为“,”,并输出显示;
str = "my name is baoabo"
str1 = str.replace(' ', ',')
print(str1)
3. 给定一个字符串str = “HELLO WORLD HELLO PYTHON”,将str中所有大写字符为小写
str = "HELLO WORLD HELLO PYTHON"
print(str.lower())
4. str = “ ”(空格),li = [“lisi”,“love“,“xuexi”],将li中每个字符后面插入str,构造出一个新 的字符串并输出
str1 = "ss"
li = ["list", "love", "xuexi"]
a = 0
while a < len(li):
li[a] = li[a] + str1
a += 1
print(li)
练习二 list
1. 静态初始化一个空列表list,使用循环向列表中添加 1–10之间的所有整数;
lists=[]
for i in range(1,11):
lists.append(i)
print(lists)
2. 向01中创建的列表list中添加一个字符串“XuanGe”
lists.append("XuanGe")
print(lists)
3. 删除下标索引为0的元素
lists.remove(lists[0])
print(lists)
lists.pop(0)
print(lists)
del lists[0]
4. 将下标索引为1的元素改为66
lists[1]=66
print(lists )
5. 输出列表中的第3个元素 作业2 list= [23,34,35,31,22,33,55,43,78,34]
list= [23,34,35,31,22,33,55,43,78,34]
print(list[3])
6:查询出所有的偶数添加到列表list1中,并将list1排序
lists1=[]
for i in list:
if i % 2==0 :
lists1.append(i)
print(lists1)
lists1.sort()
print(lists1)
7:将01中排序的list1通过extend添加list2= [[[3,21],2],[4],3],取出元素21
list2= [[[3,21],2],[4],3]
print(list2[0][0][1])

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)