#!/usr/bin/en python
#author:lijun
import
copy
names = ["
lijun
","haha
","shanshan
","heihei"]
#增刪改查
print(names[0],names[2])
print(names[1:3])
print(names[-2:0])
print(names[-2:])
print(names[:3])
"xixi")
(names)
names.insert(1,"
gaga")
(names)
names[2] = "22"
(names)
names.remove(
"lijun")
(names)
del names[1] #
names.pop(1)
(names)
names.pop()
#預設刪除最後乙個
(names)
#列表索引位置:
print(names.index("
shanshan"))
print(names[names.index("
shanshan
")])
#清空列表
#names.clear()
#print(names)
#反轉列表:
names.reverse()
(names)#排序
names.sort()
(names)
#合併列表:
name2=[1,2,3,4]
names.extend(name2)
(names)
(name2)
#刪除列表:
#del name2
#print(name2)
#複製列表:淺copy(只拷貝第一層):
name2=names.copy() #
等於name2=copy.copy(names) name2=names[:] name2=list(names)
(name2)
#拷貝不變
names[2] = "李軍"
(names)
(name2)
#列表內包括列表:拷貝只拷貝最外一層,列表內的列表只是記憶體指標,拷貝會跟隨改變
name2=names.copy()
#深copy:
name3 =copy.deepcopy(names)
(names,name2,name3)
names[2]="
lijun
"names[-1][0]=3
(names,name2,name3)
#遍歷列表:range(5)
for i in
names:
(i)for i in range(5):
(names[i])
name4="
my name is lijun
"print(name4.capitalize()) #
首字母大寫
print(name4.count("
i")) #
統計字母i的數量
print(name4.center(50,"="
))print(name4.endswith("
un")) #
判斷結尾
print(name4.expandtabs(tabsize=30))
print(name4.find("
name
")) #
索引位置
print(name4[name4.find("
name
"):]) #
切片name5="
my name is and i am old
"print(name5.format(name='
shanshan
',year=23))
print(name5.format_map())
print('
ab23
'.isalnum()) #
檢測字串是否由字母和數字組成。
print('
aba'
.isalpha())
print("1a"
.isdecimal())
print("1a"
.isdigit())
print("
1a".isidentifier()) #
判斷是不是乙個合法的識別符號(變數名)
print("
a 1a
".islower()) #
是否為小寫
print('
1'.isnumeric()) #
是否是數字
print('
'.isspace())
print('
my nmae is
'.istitle())
print('
my nmae is
'.isprintable())
print('
my nmae is
'.upper())
print(name4.ljust(50,'*'
))print(name4.rjust(50,'*'
))print("
lijun
".lower())
print("
lijun
".upper())
print("
\nlijun
".lstrip()) #
去掉左邊換行符
print("
lijun\n
".rstrip()) #
去掉右邊換行符
print("
\nlijun\n
".strip()) #
去掉前後換行符
print('
,'.join(['
1','
2','
3','
4'])) #
以,為分隔符將列表轉換成字串
p = str.maketrans("
abcdef
",'123456')
print("
alex li
".translate(p)) #
根據上面規則加密
print("
lijun li
".replace('
l','l'
))print("
lijun li
".replace('
l','
l',1))
print("
lijun li
".rfind('
l')) #
找出最右邊字母l的索引位置
print("
lijun li
".split('
l')) #
按l分隔字串,預設按空格
print("
1+2+3+4
".split('+'
))print("
1+2\n3+4
".splitlines())
print("
lijun
".swapcase())
print("
li jun
".title())
print('
li jun
'.zfill(50)) #
總共50位,不夠用0補充 用於進製
python列表字串集合常用方法
1.2 字串常用方法 1.find方法可以在乙個較長的字串中查詢子串,他返回子串所在位置的最左端索引,如果沒有找到則返回 1 a abcdefsdada print a.find abc the result 0 print a.find abc 10,100 the result 11 指定查詢的...
15 列表字串字典轉換 推導式
1.列表轉換為字串 join函式將乙個序列連線為字串 a a b 2 b str i for i in a 列表推導式,生成乙個列表,元素是str i 即將所有元素轉化為字串 c join b join函式將乙個序列連線為字串 print c2.字串轉換為列表,列表推導式,注意以把推導式廓上 d d...
python 基礎 列表,字串
url www.woaixuexi.com 1.替換 url.replace com cn print url 輸出 www.woaixuexi.cn 2.判斷是否以某個字段開頭 url.startswith www 返回 true 同startswith url.endwith com 3.查某個...