1. 程式人生 > 其它 >12.Python:字串型別

12.Python:字串型別

# str型別

a = "SEPIA" # a = str("SEPIA")

# 型別轉換
# str可以把任意其他型別轉換成字串
res = str({"a": 1})
res1 = {"a": 1}

print(res, type(res))
print(res1, type(res1))

# 使用
# (1).按索引取值(正向取,反向取):只能取
msg = "Hello world"
# 正向取
print(msg[0])
print(msg[4])
# 反向取
print(msg[-1])

# (2).切片
res = msg[0:5]
print(res)

# (3).步長
res = msg[0:5:2] # 從第0位開始取到第4位,間隔為2位
print(res)
res = msg[:] # 正向取全部
# .反向步長
res = msg[5:0:-1] # 從第5位開始取到第1位,間隔為1位
print(res)
res = msg[::-1] # 反向取全部

# .取長度
msg = "Hello world"
print(len(msg))

# .判斷字串是否在一個大字串中
x = "e"
# print("e" in msg)
print(x in msg)

# .移除空白strip
msg = " 111 222 "
res = msg.strip() # 預設去掉左右兩側的空格
print(msg)
print(res)

msg = "******111 222******"
res = msg.strip("*") # 去掉左右兩側的*
print(msg)
print(res)

# .切分split
# 把一個字串按照某種分隔符進行切分,得到一個列表
info = "SEPIA 32 male"
res = info.split() # 預設按照空格分割
print(res)

info = "SEPIA:32:male"
res = info.split(":", 1) # 預設按照":"分割, 指定分割次數
print(res)

# ————————————————————————————
# 需要掌握的操作
# strip,lstrip,rstrip
msg = "****111 222****"
print(msg.strip("*")) # 去掉兩側的"*"
print(msg.lstrip("*")) # 去掉左邊的"*"
print(msg.rstrip("*")) # 去掉右邊的"*"

# lower,upper
msg = "HELLO world"
print(msg.lower()) # 轉換成小寫
print(msg.upper()) # 轉換成大寫

# startswith,endswith
print("Hello world".startswith("Hello")) # 判斷是否以""為開頭
print("Hello world".endswith("world")) # 判斷是否以""為結尾

# split,rsplit
info = "SEPIA:32:male"
res = info.split(":", 1) # 從左到右分割, 指定分割次數
res1 = info.rsplit(":", 1) # 從右到左分割, 指定分割次數
print(res)
print(res1)

# join
l1 = ["SEPIA", "32", "male"]
res = " ".join(l1)
print(res)

# replace
msg = "you can you up, no can no bb"
res = msg.replace("can", "CAN", 1) # 替換前字串,替換後字串,替換次數
print(res)

# isdigit
# 判斷是否為純數字
age = "32"
print(age.isdigit())
print(int(age), type(int(age)))

# ————————————————————————————
# 需要了解的操作

# find
msg = "hello world"
res = msg.find("e") # 找到所查詢字元的位置,找不到時返回-1
print(res)

# index
msg = "hello world"
res = msg.index("e") # 找到所查詢字元的位置,找不到時報錯
print(res)

# count
msg = "hello world"
res = msg.count("o") # 查找出現的次數
print(res)

# center,ljust,rjust,zfill
print("SEPIA".center(20, "*")) # 列印時居中並用*填充空白
print("SEPIA".ljust(20, "*")) # 列印時左對齊並用*填充空白
print("SEPIA".rjust(20, "*")) # 列印時右對齊並用*填充空白
print("SEPIA".zfill(20)) # 列印時右對齊並用0填充空白

# expandtabs
msg = "hello\tworld"
print(msg.expandtabs(2)) # 指定製表符為2

# capitalize, swapcase, title
print("hello world".capitalize()) # 首字母大寫
print("HELLO world".swapcase()) # 大小寫反轉
print("HELLO world".title()) # 每個單詞首字母大寫

# is

num1 = b"4"
num2 = u"4"
num3 = "四"
num4 = "Ⅳ"
# isdigit識別數字
print(num1.isdigit()) # true
print(num2.isdigit()) # true
print(num3.isdigit()) # false
print(num4.isdigit()) # false

# isnumeric識別數字,只能識別num2,num3,num4
print(num2.isnumeric()) # true
print(num3.isnumeric()) # true
print(num4.isnumeric()) # true

# isdecimal識別數字,只能識別num2
print(num2.isdecimal()) # true
print(num3.isdecimal()) # false
print(num4.isdecimal()) # false

# -----------------------------------------
# is瞭解
# print("abc".islower()) # 判斷小寫
# print("ABC".isupper()) # 判斷大寫
# print("Abc Def".istitle()) # 判斷首字母大寫
# print("123".isalnum()) # 判斷數字
# print("abc".isalpha()) # 判斷字母
# print(" ".isspace()) # 判斷空格
# print("abc".isidentifier()) # 判斷名字是否合法