🌌 專注Golang,Python語言,云原生,人工智能領域得博主;
💜 過去經歷的意義在于引導你,而非定義你;
📢 歡迎點贊 👍 收藏 ?留言!

前言
- 字串
- 下標
- 切片
- 字串查找相關的操作
- 字串的替換
- 字串的分隔
- 字串連接
- 串列
- 串列遍歷
- 向串列添加資料的方法
- 串列中的資料查詢操作
- 串列洗掉操作
- 串列中的排序操作
- 串列的嵌套
- 元祖
字串
# 單引號
name = 'isaac'
print(type(name), name)
# 雙引號
name = "isaac"
print(type(name), name)
# 三引號
my_str = """hello world
hello python!
"""
print(type(my_str))
my_str = '''aaa
bbb
'''
print(type(my_str))
# 如果字串本身包含單引號,使用雙引號定義,如果包含雙引號可以使用單引號定義,或者統一使用三引號引號
# my name is 'isaac'
my_name = "my name is 'isaac'"
下標
# 下標也稱為是索引,是一個整型數字,可以是正數, 也可以是負數
# 正數下標是從0開始的,表示第一個字符, -1 表示最后一個字符
my_str = 'hello'
# 下標的使用語法 變數[下標]
print(my_str[0]) # h
print(my_str[1]) # e
print(my_str[-1]) # o
print(my_str[-3]) # l
# len() 函式可以得到字串的長度
print(len(my_str)) # 5
# 使用正數下標書寫字串中最后一個元素
print(my_str[len(my_str) - 1])
print(my_str[len(my_str) * (-1)])
切片
# 切片可以獲取一段資料,多個資料, 下標(索引只能獲得一個資料)
# 切片語法: 變數[start:end:step], 會得到一個新的字串
# start 開始位置的下標
# end 結束位置的下標, 不包含end 對應的下標
# step 步長,下標之間的間隔,默認是1
my_str = 'hello'
my_str1 = my_str[2:4:1] # ll
print(my_str1)
# step 如果是1 ,即默認值,可以不寫
my_str2 = my_str[2:4] # ll
print(my_str2)
# end 位置不寫, 表示是len() , 即可以取到最后一個元素
my_str3 = my_str[2:] # llo
print(my_str3)
# start 位置也可以省略不寫, 表示是0,
my_str4 = my_str[:3] # hel
print(my_str4)
# start 和end 都不寫,但是冒號需要寫
my_str5 = my_str[:] # hello
print(my_str5)
print(my_str[-4: -1]) # ell
print(my_str[3:1], '1') # 沒有資料,
# 步長可以是負數
print(my_str[3:1:-1], '2') # ll
print(my_str[::-1]) # 字串的逆置, olleh
print(my_str[::2]) # 0 2 4 hlo my_str[0:5:2]
print(my_str[1:4:-1])
字串查找相關的操作
my_str = 'hello world itcast and itcastcpp'
# find() 在字串中查找是否存在某個字串
# my_str.find(sub_str, start, end)
# sub_str: 要在字串中查找的內容, 型別 str
# start: 開始位置,從哪里開始查找, 默認是0
# end: 結束的位置,查找到哪里結束, 默認是len()
# 回傳值:即方法執行的結果是什么, 如果找到 sub_str ,回傳的sub_str 在 my_str 中的位置的正數下標
# 如果沒有找到,回傳 -1
index = my_str.find('hello') # 0
print(index)
# 從下標為3的位置,開始查找字串 hello
print(my_str.find('hello', 3)) # -1
print(my_str.find('itcast')) # 12
print(my_str.find('itcast', 15)) # 23
# rfind() right find() 從右邊(后邊) 開始查找
print(my_str.rfind('itcast')) # 23
# index() 在字串中查找是否存在某個字串
# my_str.index(sub_str, start, end)
# sub_str: 要在字串中查找的內容, 型別 str
# start: 開始位置,從哪里開始查找, 默認是0
# end: 結束的位置,查找到哪里結束, 默認是len()
# 回傳值:即方法執行的結果是什么, 如果找到 sub_str ,回傳的sub_str 在 my_str 中的位置的正數下標
# 如果沒有找到,會報錯
print(my_str.index('hello')) # 0
# print(my_str.index('hello', 3)) # 沒有找到,代碼會報錯
# rindex() 從后邊開始查找
print(my_str.index('itcast')) # 12
print(my_str.rindex('itcast')) # 23
# count(sub_str, start, end) 統計出現的次數,
print(my_str.count('aaaa')) # 0
print(my_str.count('hello')) # 1
print(my_str.count('itcast')) # 2
print(my_str.count('itcast', 20)) # 1
字串的替換
# my_str.replace(old_str, new_str, count) 字串的替換, 將my_str中的 old_str 替換成 new_str
# old_str: 將要被替換的字串
# new_str: 新的字串, 替換成的字串
# count: 替換的次數,默認是全部替換
# 回傳值: 得到一個新的字串,不會改變原來的字串
my_str = 'hello world itcast and itcastcpp'
my_str1 = my_str.replace('itcast', 'itheima')
print('my_str :', my_str)
print('my_str1:', my_str1)
my_str2 = my_str.replace('itcast', 'itheima', 1) # 替換一次
print('my_str2:', my_str2)
字串的分隔
my_str = 'hello world itcast and itcastcpp'
# my_str.split(sub_str, count) 將my_str 字串按照sub_str 進行切割,
# sub_str: 按照什么內容切割字串, 默認是空白字符, 空格, tab鍵
# count: 切割幾次,默認是全部切割
# 回傳值: 串列 []
result = my_str.split() # 按照空白字符,全部切割
print(result)
print(my_str.split('itcast'))
print(my_str.split('itcast', 1)) # 切割一次,
print(my_str.rsplit('itcast', 1))
字串連接
# my_str.join(可迭代物件)
# 可迭代物件, str, 串列(需要串列中的每一個資料都是字串型別)
# 將my_str 這個字串添加到可迭代物件的兩個元素之間
# 回傳值: 一個新的字串, 不會改變原字串的值
my_str = '_'.join('hello') # 會把_ 加入到 hello每兩個元素之間 即 h_e_l_l_o
print(my_str)
print('_*_'.join('hello')) # h_*_e_*_l_*_l_*_o
# 定義串列
my_list = ['hello', 'cpp', 'python']
print("_".join(my_list)) # hello_cpp_python
print("_*_".join(my_list)) # hello_*_cpp_*_python
print(" ".join(my_list)) # hello cpp python
串列
# 串列 是python中的一種資料型別,可以存放多個資料,串列中的資料可以是任意型別的
# 串列 list ,定義使用[] 進行定義
# 定義空串列
my_list = []
print(my_list, type(my_list))
my_list1 = list() # 空串列
print(my_list1, type(my_list1))
# 定義帶資料的串列, 資料元素之間使用逗號隔開
my_list2 = [1, 3.14, True, 'isaac']
print(my_list2, type(my_list2))
# 求串列中資料元素的個數,即串列的長度
num = len(my_list2)
print(num)
# 串列支持下標和切片操作
print(my_list2[1]) # 3.14
print(my_list2[-1]) # isaac
print(my_list2[1:3]) # [3.14, True]
# 下標操作和字串中不同的是: 字串不能使用下標修改其中的資料, 但是串列可以使用下標修改串列中的資料
my_list2[0] = 18
print(my_list2)
my_list2[-1] = 'hello'
print(my_list2)
my_list2[0] = 'python'
print(my_list2)
串列遍歷
my_list = ['郭德綱', '于謙', '小岳岳', '孫越']
for i in my_list: # i 就是串列中的每一個資料
print(i)
print('*' * 30)
j = 0 # j 表示下標
while j < len(my_list):
print(my_list[j])
j += 1
向串列添加資料的方法
# 向串列中添加資料的方法,都是直接在原串列中進行添加的,不會回傳新的串列
my_list = ['郭德綱', '于謙', '小岳岳', '孫越']
print(my_list)
# 串列.append(資料) 向串列的尾部追加資料
my_list.append('aa')
print(my_list)
result = my_list.append(12) # 不要這樣書寫.
# print(result) # None 關鍵字,表示空,
print(my_list)
# 串列.insert(下標, 資料) 在指定的下標位置進行添加資料
my_list.insert(0, 'isaac')
print(my_list)
# print(my_list.insert(5, 3.14)) 不能這樣書寫, None
# 串列.extend(可迭代物件) # str 串列, 會將可迭代物件中的資料逐個添加到原串列的末尾
my_list.extend('hel')
print(my_list)
my_list.extend([1, 'python', 3])
print(my_list)
串列中的資料查詢操作
my_list = [1, 3.14, 'isaac', False]
# index() 根據資料值,查找元素所在的下標, 找到回傳元素的下標, 沒有找到,程式報錯
# 串列中沒有find方法,只有index() 方法
# 查找 3.14 在串列中下標
num = my_list.index(3.14) # 1
print(num)
# num1 = my_list.index(100) # 程式報錯, 因為資料不存在
# count() 統計出現的次數
num3 = my_list.count(1) # 1
print(num3)
# in /not in 判斷是否存在, 存在是True, 不存在是False,一般和if 結合使用
num4 = 3.14 in my_list # True
print(num4)
num4 = 3.14 not in my_list # False
print(num4)
串列洗掉操作
my_list = [1, 2, 4, 5, 6, 9]
# 1. 根據元素的資料值洗掉 remove(資料值), 直接原串列中的資料
my_list.remove(4)
print(my_list) # [1, 2, 5, 6, 9]
# my_list.remove(4) # 程式報錯,要洗掉的資料不存在
# 2. 根據下標洗掉
# 2.1 pop(下標) 默認洗掉最后一個資料,回傳洗掉的內容
num = my_list.pop() # 洗掉最后一個資料 9
print(num)
print(my_list) # [1, 2, 5, 6]
num = my_list.pop(2) # 洗掉下標為2的資料即 5
print(num)
print(my_list) # [1, 2, 6]
# my_list.pop(10) # 洗掉的下標不存在,會報錯
# 2.2 del 串列[下標]
del my_list[1] # 洗掉下標為1的資料 2
print(my_list)
# del my_list[10] # 洗掉不存在的下標, 會報錯
串列中的排序操作
# 想要對串列中的資料進行排序,前提是串列中的資料型別是一樣的
my_list = [1, 5, 3, 7, 9, 6]
# 串列.sort() 直接在原串列中進行排序
# my_list.sort() # 默認是從小到大排序, 即升序
# my_list.sort(reverse=True) # 通過reverse=True,從大到小排序
print(my_list)
# 補充: sorted(串列) 排序, 不會在原串列中進行排序,會得到一個新的串列
my_list1 = sorted(my_list)
my_list2 = sorted(my_list, reverse=True)
print(my_list)
print(my_list1)
print(my_list2)
print("=" * 30)
my_list3 = ['a', 'b', 'c', 'd', 'e']
# 逆置,
my_list4 = my_list3[::-1] # 得到一個新的串列
print(my_list3)
print(my_list4)
# 在原串列直接逆置 串列.reverse()
my_list3.reverse()
print(my_list3)
串列的嵌套
school_names = [['北京大學', '清華大學'],
['南開大學', '天津大學', '天津師范大學'],
['山東大學', '中國海洋大學']]
print(school_names[1]) # ['南開大學', '天津大學', '天津師范大學']
print(school_names[1][1]) # 天津大學
print(school_names[1][1][1]) # 津
# 山東大學
print(school_names[2][0])
for schools in school_names:
# print(schools) # 串列
for name in schools:
print(name)
元祖
# 元組和串列非常相似, 都可以存放多個資料,可以存放不同資料型別的資料
# 不同點: 串列使用 [] 定義, 元組使用 () 定義
# 串列中的資料可以修改,元組中的資料不能被修改
my_list = [18, 3.14, True, 'isaac'] # 串列
my_tuple = (18, 3.14, True, 'isaac') # 元組
print(my_tuple, type(my_tuple))
# 元組支持下標和切片操作
print(my_tuple[1]) # 3.14
# 定義空元祖, 沒有意義
my_tuple1 = ()
print(my_tuple1, type(my_tuple1))
my_tuple2 = tuple()
# 定義一個資料元素的元組, 資料元素后邊,必須有一個逗號
my_tuple3 = (3) # 3 <class 'int'>
my_tuple4 = (3,)
print(my_tuple3, type(my_tuple3))
print(my_tuple4, type(my_tuple4))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/310580.html
標籤:python
