python学习3——字符串、列表
来源:互联网 发布:linux中的samba下载 编辑:程序博客网 时间:2024/05/18 00:02
一、字符串
程序:获取数据——处理——输出
(1)切片
>>> print(name[0:3]) #3取不到
hel
>>> print(name[0:4]) #4取不到
hell
>>> print(name[2:4])
ll
>>> print(name[0:6:1])
hellob
>>> print(name[0:6:2])
hlo
>>> name="laoluo"
>>>
>>> name[0]
'l'
>>> name[-1]
'o'
>>> name[-2]
'u'
>>> name="abcdefg"
>>> name[-1:-5:-1]
'gfed'
>>> name[::-1]
'gfedcba'
(2)字符串操作——find
>>> mystr='hello world itcast and itcastcpp'
>>> mystr
'hello world itcast and itcastcpp'
>>> mystr.find("it") #从左往右找
12
>>> mystr.find("itcast",14,20)
-1 #没有找到
>>> mystr.find("itcast",14,200)
23
>>> mystr.rfind("itcast") #从右往左找
23
(3)字符串操作——index(找不到就产生异常)
>>> mystr.index("xxx")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
找文件后缀名
>>> myFileName=("sdsakhdslkdjsdfsd.py")
>>> myFileName.rfind(".")
17
>>> index=myFileName.rfind(".")
>>> index
17
>>> myFileName[17:]
'.py'
(4)字符串操作——count
>>> mystr='hello world itcast and itcastcpp'
>>> mystr.count("itcast")
2
(5)字符串操作——replace
>>> mystr.replace("it","IT") #全部替换
'hello world ITcast and ITcastcpp'
>>> mystr #一般不赋值,mystr里内容不变
'hello world itcast and itcastcpp'
>>> mystr.replace("it","IT",1) #制定替换次数
'hello world ITcast and itcastcpp'
(6)字符串操作——split
>>> str1="haha heihei hehehehe"
>>> str1.split(" ")
['haha', 'heihei', 'hehehehe']
>>> str1.split(" ",1)
['haha', 'heihei hehehehe']
(7)字符串操作——capitalize首字母大写
>>> mystr.capitalize()
'Hello world itcast and itcastcpp'
>>>
(8)字符串操作——upper全部变大写
>>> mystr.upper()
'HELLO WORLD ITCAST AND ITCASTCPP'
>>> mystr2=mystr.upper() #存储
(9)字符串操作——lower全部变小写
>>> mystr2.lower()
'hello world itcast and itcastcpp'
(10)字符串操作——ljust靠左
mystr.ljust(10)
(11)字符串操作——rstrip删除mystr字符末尾的空白字符
>>> a = ' hello '
>>> a
' hello '
>>> a.strip()
'hello'
(11)字符串操作——partition
>>> mystr.partition("and")
('hello world itcast ', 'and', ' itcastcpp')
>>> mystr.split("and")
['hello world itcast ', ' itcastcpp']
>>> mystr.rpartition("itcast")
('hello world itcast and ', 'itcast', 'cpp')
二、列表
1、数字和字符串都可以保存
a=[11,222,"asdsd"]
print(a[0]) #11
2题目:输出文件类型
方法一
fileNames = ['01.py','02.py','03.txt','05.rar','06.php']
for tempName in fileNames:
position = tempName.rfind(".")
print(tempName[position:])
方法二
fileNames = ['01.py','02.py','03.txt','05.rar','06.php']
i = 0
length = len(fileNames)
while i<length:
tempName = fileNames[i]
position = tempName.rfind(".")
print(tempName[position:])
i+=1
3.列表操作
(1)增
names = ['abc','bsc','dddd']
for temp in names:
print(temp)
print("=============")
names.append('xiaohua')
for temp in names:
print(temp)
#encoding=utf-8
names = ["abc","bsc","dddd"]
for x in names:
print(x)
print("="*30)
newName = input("请输入要添加的名字")
names.append(newName)
for x in names:
print(x)
(2)插入(特定位置)
#encoding=utf-8
names = ["abc","bsc","dddd"]
for x in names:
print(x)
print("="*30)
newName = input("请输入要添加的名字")
names.insert(0,newName)
for x in names:
print(x)
(3)扩展
>>> a=[1,2]
>>> b=["a","b","c"]
>>> a.extend(b)
>>> a
[1, 2, 'a', 'b', 'c']
(4)修改
>>> names = ['aaa','b','cccc']
>>> names
['aaa', 'b', 'cccc']
>>>
>>>
>>> names[1]
'b'
>>> names[1] = "bbb"
>>> names[1]
'bbb'
(5)
#encoding=utf-8
fileNames = ["01.py","02.txt","03.rar","04.c","05.cpp"]
for temp in fileNames:
if "01.py" == temp:
print("找到了")
- python学习3——字符串、列表
- python学习3——列表
- 【Python】基础学习——列表(list),元祖(tuple),字典(dict),字符串(string),集合(set)
- python学习(2)——字符串,列表,元组,集合
- python 学习笔记:字符串,列表,元组
- python学习第一章(数字,字符串,列表等)
- python基础学习_字符串,列表,字典
- Python学习之比较列表和字符串
- Python学习笔记(1)字符串&列表&条件
- python列表学习3
- <3>python学习笔记——列表 / 元组
- python学习笔记3——列表和元组
- python学习总结——列表
- python系列学习一 —— 列表
- Python学习笔记——序列、列表
- Python学习笔记 —— List列表
- python学习——列表生成式
- Python 学习笔记(2)——列表
- linux清理
- [牛客]词语变形练习题
- pthread是什么
- 投简历到底应该“高攀”还是“低嫁”?
- 软件测试自学指南---从入门到精通V2.0
- python学习3——字符串、列表
- Java Web 获取客户端真实IP
- 学习笔记——java中重写和重载的在实际开发的应用场景
- NYOJ 711 最舒适的路线(bfs+优先队列)
- 【精】OkHttp3源码分析[综述]
- RequestMapping
- 【HTML5入门】解决eclipse中tomcat8080端口被占用问题
- C# 在excel中查找及替换数据
- Mac OS搭建Tensorflow环境