1. 程式人生 > 其它 >一木.溪橋學Python-05: str、list、bytes

一木.溪橋學Python-05: str、list、bytes

技術標籤:Python學習之路python

一木.溪橋 在Logic Education跟Amy學Python

12期:Python基礎課
一木.溪橋學Python-05: str、list、bytes
日期:2020年12月23日


學習目標:

  • 字串:str
  • 位元組:bytes
  • 列表:list

學習內容:

字串:str

字串常見操作 ( 熟悉 )
S.find(sub) --> 返回該元素最小的索引
S.index(sub) --> 返回該元素最小的索引
S.replace(old, new[, count]) --> 替換
S.split(sep=None) --> 以 sep 來分割字串 , 並返回列表。 sep 預設為 None, 分割預設為空格

S.startswith(prefix[, start[, end]]) --> 判斷字串是否以字首開始,返回為 bool 值。
S.endswith(suffix[, start[, end]]) --> 判斷字串是否以尾綴結束,返回為 bool 值。
S.lower() --> 將字串全部轉為小寫
S.upper() --> 將字串全部轉為大寫
S.strip([chars]) --> 預設去掉字串左右的空格
S.isalpha() --> 判斷字串是否全為字母,返回的是 bool 值
S.isdigit() --> 判斷字串是否全為數字,返回的是 bool 值
S.isalnum() --> 判斷字串是否全為數字或者字母,不存在特殊字元,返回的是 bool 值
S.join(iterable) --> 將序列中的元素以指定的字元連線生成一個新的字串

  • S.find(sub) -->返回的是sub的最小索引,如果S中沒有sub值則返回-1.
s = "hello everyone !"

print(s.find("e"))   # 列印e的最小索引,返回1

print(s.find("E"))   # 沒有E,返回-1,不報錯
  • S.index(sub) -->返回的是sub的最小索引,如果S中沒有sub值則報錯.
s = "hello everyone !"

print(s.index("e"))   # 列印e的最小索引,返回1

print(s.index("E"))   # 沒有E,報錯
  • S.replace(old, new[, count]) --> 替換 replace(self, old, new, count=None)
s = "hello everyone !"

# print(s.replace("e","A"))   # 預設替換全部

print(s.replace("e","A",2))   # 指定替換次數為2;hAllo Averyone !
  • S.split(sep=None) --> 以 sep 來分割字串 , 並返回列表。 sep 預設為 None, 分割預設為空格
s = "hello everyone !"

print(s.split(" "))   # 用空格分割字串S,返回為列表:['hello', 'everyone', '!']
  • S.startswith(prefix[, start[, end]]) --> 判斷字串是否以字首開始,返回為 bool 值。
s = "hello everyone !"

print(s.startswith("h"))   # 判斷字串S是否以h開始,返回為bool:True
  • S.endswith(suffix[, start[, end]]) --> 判斷字串是否以尾綴結束,返回為 bool 值。
s = "hello everyone !"

print(s.endswith("!"))   # 判斷字串S是否以!結束,返回為bool:True
  • S.lower() --> 將字串全部轉為小寫
s = "hello everyone !"

s1 = s.upper()             # 字串s轉為大寫:HELLO EVERYONE !

s2 = s1.lower()            # 字串s1轉為小寫:hello everyone !

print(s2)                  

print(id(s))               # id(s):2677845944192

print(id(s2))              # id(s):2677845944408
  • S.upper() --> 將字串全部轉為大寫
s = "hello everyone !"

print(s.upper())   # 字串S轉為大寫:HELLO EVERYONE !
  • S.strip([chars]) --> 預設去掉字串左右的空格
s = "  hello everyone !  "

print(s.strip())               # 去掉字串S首尾的空格:hello everyone !

print(s.replace(" ", "*"))     # 也可用replace功能替換想替換的:**hello*everyone*!**
  • S.isalpha() --> 判斷字串是否全為字母,返回的是 bool 值
s = "helloeveryone"

print(s.isalpha())               # :True
  • S.isdigit() --> 判斷字串是否全為數字,返回的是 bool 值
s = "145632f"

print(s.isdigit())               # :False
  • S.isalnum() --> 判斷字串是否全為數字或者字母,不存在特殊字元,返回的是 bool 值
s = "145d"

print(s.isalnum())               # :True
s = "145d!"

print(s.isalnum())               # :False
  • S.join(iterable) --> 將序列中的元素以指定的字元連線生成一個新的字串
s = "hello everyone !"

print("*".join(s))               # :h*e*l*l*o* *e*v*e*r*y*o*n*e* *!

位元組:bytes

  • 位元組介紹 ( 掌握 )
    在 Python3 以後,字串 和 bytes 型別徹底分開了。字串 是以 字元 為單位進行處
    理的,bytes 型別是以 位元組 為單位處理的。
    bytes 資料型別在所有的操作和使用甚至內建方法上和字串資料型別基本一樣,也是
    不可變的序列物件。
    Python3 中,bytes 通常用於網路資料傳輸、二進位制圖片和檔案的儲存等等。
  • 位元組建立 ( 掌握 )
    可以通過呼叫 bytes() 生成 bytes 例項,其值形式為 b’xxxxx’ ,對於同一個字串如
    果採用不同的編碼方式生成 bytes 物件,就會形成不同的值。
bt_1 = b"hello everyone !"

print(bt_1)               # b'hello everyone !'

print(type(bt_1))         # <class 'bytes'>
###
bt_2 = bytes("hello everyone !", encoding="utf_8")

print(bt_2)               # b'hello everyone !'

print(type(bt_2))         # <class 'bytes'>
  • 位元組與字串轉換
    bytes to str --> .decode() 解碼
    str to bytes --> .encode() 編碼
bt_2 = bytes("hello", encoding="utf")

s = bt_2.decode()

print(s)               # hello

print(type(s))         # <class 'str'>

bt_3 = s.encode()

print(bt_3)               # b'hello'

print(type(bt_3))         # <class 'bytes'>

列表:list

列表介紹 ( 掌握 )
列表是 Python 中最基本也是最常用的資料結構之一,它是一個 有序可重複的元素
集合。從資料結構角度看,Python 的列表是一個 可變長度 的順序儲存結構,每一
個位置存放的都是物件的指標。
我們可對列表進行 修改、切片、追加、刪除、巢狀、迭代、成員判斷 等操作。

  • 列表建立 ( 掌握 )
    建立一個列表,只要把 逗號 分隔的 不同的資料元素 使用 方括號 括起來即可。
li = ["good",2,3,4,2,6.6]

print(li)                   # ['good', 2, 3, 4, 2, 6.6] 列表中的元素型別任意,元素可重複。

print(type(li))             # <class 'list'>
li = list("hello")

print(li)                   # ['h', 'e', 'l', 'l', 'o']  list(iterable) 可迭代

print(type(li))             # <class 'list'>

作業:


作業答案: