字符串大小写
name=“hufeng”
print(name)#原样输出
print(name.title()#字符串首字母大写
print(name.upper())#字符串全部大写
print(name.lower())#字符串全部小写
input()
字符串空格
#字符串空格
name=’ python ’
print(name.lstrip())#删除前面的可空格
print(name.rstrip())#删除后面的空格
print(name.strip())#前后空格都删除
input()
字符串的显示输出
#字符串的显示输出
a=23
print(“happy”+int(a))#a定义时没指定类型,输出时要指定
print(“happy”+str(a))#
input()
print()打印东西时,参数是引号引起来就原样输出,不是就一般是变量,那么就看变量对应的操作。
添加空格用制表符\t;删除空格用函数*tri()。
输出字符串中有单引号时用双引号包裹输出;输出字符串中有双引号时用单引号包裹输出。
列表中的0和-1
#列表中的0和-1
words=[‘a’,‘b’,‘c’,‘d’]
print(words[0])
print(words[-1])#-1直接输出最后一个
input()
列表元素修改
#列表元素修改
words=[‘a’,‘b’,‘c’,‘d’]
words[0]=‘s’#注意引号
print(words)
input()
列表末尾元素添加
#列表末尾元素添加
words=[‘a’,‘b’,‘c’,‘d’]
words.append(‘s’)
print(words)
input()
列表任意位置元素添加
#列表任意位置元素添加
words=[‘a’,‘b’,‘c’,‘d’]
words.insert(0,‘s’)#在索引0的位置添加;不是替换‘a’
print(words)
input()
列表精准元素删除
#列表精准元素删除
words=[‘a’,‘b’,‘c’,‘d’]
del words[0]
print(words)
input()
列表pop()出栈删除
#列表pop()出栈删除
words=[‘a’,‘b’,‘c’,‘d’]
new_words=words.pop()
print(words)
print(new_words)#出栈出最后一个进列表的
input()
列表pop()出栈删除利用
#列表pop()出栈删除利用
words=[‘a’,‘b’,‘c’,‘d’]
new_words=words.pop(3)
print(new_words)#弹出最后一个元素
input()
列表remove()判断删除
#列表remove()判断删除
words=[‘a’,‘b’,‘c’,‘c’,‘d’]
new_words=words.remove(‘c’)
print(words)#只删除了第一个’c’
print(new_words)#结果为None说明remove没有返回值
input()
列表永久排序
#列表永久排序
words=[‘a’,‘f’,‘c’,‘d’]
words.sort()
print(words)
words.sort(reverse=True)
print(words)
input()
列表暂时排序
#列表暂时排序
words=[‘a’,‘f’,‘c’,‘d’]
new_words=sorted(words)
print(new_words)#sorted有返回值
print(words)
input()
列表永久倒序打印
#列表永久倒序打印
words=[‘a’,‘f’,‘c’,‘d’]
print(words)
words.reverse()
print(words)
input()
列表长度
#列表长度
words=[‘a’,‘f’,‘c’,‘d’]
length=len(words)
print(length)
input()
遍历列表元素
#遍历列表元素
words=[‘a’,‘f’,‘c’,‘d’]
for word in words:print(word)#不能将print(word)写到下一行
#print(word)这里会显示expected an indented block缩进问题
input()
创建数值列表
#创建数值列表
for value in range(1,6):print(value)
number=list(range(1,6))print(number)
input()
列表数据处理
#列表数据处理
number=[1,5,9,8,7,3,2,25]
print(max(number))
print(min(number))
print(sum(number))
input()