一、字符串
程序:获取数据——处理——输出
(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 "
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
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("找到了")