1.用途:描述性質的東西,如人的名字、單個愛好、地址、國家等git
2.定義:使用''、""、''''''、""""""包裹的的一串字符api
name = 'nick' # name =str('nick') s1 = str(1.1) s2 = str([1, 2, 3]) print(f's1:{s1}, type:{type(s1)}') print(f's2:{s2}, type:{type(s2)}')
s1:1.1, type:<class 'str'> s2:[1, 2, 3], type:<class 'str'>
3.經常使用操做+內置方法:經常使用操做和內置方法分爲優先掌握(今天必須得記住)、須要掌握(一週內記住)、其餘操做(瞭解)三個部分。編碼
按索引取值spa
切片code
長度len索引
成員運算in|not inip
移除空白stripci
切分splitunicode
循環
1.按索引取值(只可取不可改變)
# str索引取值 msg = 'hello nick' # 0123456789 # 索引序號 print(f'索引爲6: {msg[6]}') print(f'索引爲-3: {msg[-3]}')
索引爲6: n 索引爲-3: i
2.切片(顧頭不顧尾,步長)
# 索引切片 msg = 'hello nick' # 0123456789 # 索引序號 print(f'切片3-最後: {msg[3:]}') print(f'切片3-8: {msg[3:8]}') print(f'切片3-8,步長爲2: {msg[3:8:2]}') print(f'切片3-最後,步長爲2: {msg[3::2]}') # 瞭解,步長爲正從左到右;步長爲負從右到左 print('\n**瞭解知識點**') print(f'切片全部: {msg[:]}') print(f'反轉全部: {msg[::-1]}') print(f'切片-5--2: {msg[-5:-2:1]}') print(f'切片-2--5: {msg[-2:-5:-1]}')
切片3-最後: lo nick 切片3-8: lo ni 切片3-8,步長爲2: l i 切片3-最後,步長爲2: l ik **瞭解知識點** 切片全部: hello nick 反轉全部: kcin olleh 切片-5--2: ni 切片-2--5: cin
3.長度len
# str長度 msg = 'hello nick' print(len(msg))
10
4.成員運算in和not in
# str成員運算 msg = 'my name is nick, nick handsome' print(f"'nick' in msg: {'nick' in msg}") print(f"'jason' not in msg: {'jason' not in msg}") print(f"not 'jason' in msg: {not 'jason' in msg}")
'nick' in msg: True 'jason' not in msg: True not 'jason' in msg: True
5.移除空白strip()
# str移除空白strip() name = '&&&n ick' print(f"name.strip('&'): {name.strip('&')}") # strip()默認爲‘ ’,而且不修改原值,新建立空間 print(f"name: {name}") # strip()應用場景 pwd = input('password: ') # 用戶可能會手抖輸入空格 if pwd.strip() == '123': print('密碼輸入成功') print(f"'*-& nick+'.strip('*-& +'): {'*-& nick+'.strip('*-& +')}")
name.strip('&'): n ick name: &&&n ick password: 123 密碼輸入成功 '*-& nick+'.strip('*-& +'): nick
6.切分split
# str切分split info = 'nick:male:19' info_list1 = info.split(':') info_list2 = info.split(':', 1) print(f'info_list1:{info_list1}') print(f'info_list2:{info_list2}')
info_list1:['nick', 'male', '19'] info_list2:['nick', 'male:19']
7.循環
msg = 'hello nick' for i in msg: print(i)
h e l l o n i c k
lstrip&rstrip
lower&upper
startswith&endswith
rsplit
join
replace
isdigit
1.lstrip()和rstrip()
# str之lstrip()和rstrip() name = '&&nick&&' print(f"nick.lstrip('&'): {name.lstrip('&')}") print(f"nick.rstrip('&'): {name.rstrip('&')}")
nick.lstrip('&'): nick&& nick.rstrip('&'): &&nick
2.lower()和upper()
# str之lower()和upper() name = 'Nick Chen' print(f"name.upper(): {name.lower()}") print(f"name.upper(): {name.upper()}")
name.upper(): nick chen name.upper(): NICK CHEN
3.startswith()和endswith()
# str之startswith()和endswith() name = 'Nick Chen' print(f"name.startswith('Nick'): {name.startswith('Nick')}") print(f"name.endswith('chen'): {name.endswith('chen')}")
name.startswith('Nick'): True name.endswith('chen'): False
4.rsplit()
# str之rsplit() info = 'nick:male:19' print(f"info.rsplit(':', 1): {info.rsplit(':', 1)}") # 從右開始切割
info.rsplit(':', 1): ['nick:male', '19']
5.join()
lis = [1,2,'19'] print(f"':'.join(lis): {':'.join(lis)}") # 報錯,數字不可和字符串拼接
# str之join() lis = ['nick', 'male', '19'] print(f"':'.join(lis): {':'.join(lis)}")
':'.join(lis): nick:male:19
6.replace()
# str值replace() name = 'nick shuai' print(f"name.replace('shuai','handsome'): {name.replace('shuai','handsome')}")
name.replace('shuai','handsome'): nick handsome
7.isdigit()
# str值isdigit() salary = '111' print(salary.isdigit()) # True salary = '111.1' print(salary.isdigit()) # False
True False
# str之isdigit()應用場景 age = input('age: ') if age.isdigit(): age = int(age) if age < 18: print('小姐姐') else: print('阿姨好') else: print(f'你的年齡能是這個{age}?')
age: 逗你玩? 你的年齡能是這個逗你玩??
find|rfind|index|rindex|count
center|ljust|rjust|zfill
expandtabs
captalize|swapcase|title
is系列
1.find()、rfind()、index()、rindex()、count()
# str之find()、rfind()、index()、rindex()、count() msg = 'my name is tank, tank shi sb, hha' print(f"msg.find('tank'): {msg.find('tank')}") # 找不到返回-1 print(f"msg.find('tank',0,3): {msg.find('tank',0,3)}") print(f"msg.rfind('tank'): {msg.rfind('tank')}") # 找不到返回-1 print(f"msg.index('tank'): {msg.index('tank')}") # 找不到報錯 print(f"msg.rindex('tank'): {msg.rindex('tank')}") # 找不到報錯 print(f"msg.count('tank'): {msg.count('tank')}")
msg.find('tank'): 11 msg.find('tank',0,3): -1 msg.rfind('tank'): 17 msg.index('tank'): 11 msg.rindex('tank'): 17 msg.count('tank'): 2
2.center()、ljust()、rjust()、zfill()
# str之center()、ljust()、rjust()、zfill() print(f"'info nick'.center(50,'*'): {'info nick'.center(50,'*')}") print(f"'info nick'.ljust(50,'*'): {'info nick'.ljust(50,'*')}") print(f"'info nick'.rjust(50,'*'): {'info nick'.rjust(50,'*')}") print(f"'info nick'.zfill(50): {'info nick'.zfill(50)}") # 默認用0填充
'info nick'.center(50,'*'): ********************info nick********************* 'info nick'.ljust(50,'*'): info nick***************************************** 'info nick'.rjust(50,'*'): *****************************************info nick 'info nick'.zfill(50): 00000000000000000000000000000000000000000info nick
3.expandtabs()
# str之expandtabs() print(f"a\\tb\\tc: %s"%('a\tb\tc\t')) # 默認製表符8個空格 print(f"'a\\tb\\tc'.expandtabs(32): %s"%('a\tb\tc\t'.expandtabs(32)))
a\tb\tc: a b c 'a\tb\tc'.expandtabs(32): a b c
4.captalize()、swapcase()、title()
# str之captalize()、swapcase()、title() name = 'nick handsome sWAPCASE' print(f"name.capitalize(): {name.capitalize()}") print(f"name.swapcase(): {name.swapcase()}") # 大小寫互轉 print(f"name.title(): {name.title()}")
name.capitalize(): Nick handsome swapcase name.swapcase(): NICK HANDSOME Swapcase name.title(): Nick Handsome Swapcase
5.is數字系列(只是爲了告訴你,判斷是否爲數字時除了中文數字之後使用isdigit()便可)
num = "1" #unicode num.isdigit() # True num.isdecimal() # True num.isnumeric() # True num = "1" # 全角 num.isdigit() # True num.isdecimal() # True num.isnumeric() # True num = b"1" # byte num.isdigit() # True num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal' num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric' num = "IV" # 羅馬數字 num.isdigit() # True num.isdecimal() # False num.isnumeric() # True num = "四" # 漢字 num.isdigit() # False num.isdecimal() # False num.isnumeric() # True =================== isdigit() True: Unicode數字,byte數字(單字節),全角數字(雙字節),羅馬數字 False: 漢字數字 Error: 無 isdecimal() True: Unicode數字,全角數字(雙字節) False: 羅馬數字,漢字數字 Error: byte數字(單字節) isnumeric() True: Unicode數字,全角數字(雙字節),羅馬數字,漢字數字 False: 無 Error: byte數字(單字節) ================ import unicodedata unicodedata.digit("2") # 2 unicodedata.decimal("2") # 2 unicodedata.numeric("2") # 2.0 unicodedata.digit("2") # 2 unicodedata.decimal("2") # 2 unicodedata.numeric("2") # 2.0 unicodedata.digit(b"3") # TypeError: must be str, not bytes unicodedata.decimal(b"3") # TypeError: must be str, not bytes unicodedata.numeric(b"3") # TypeError: must be str, not bytes unicodedata.digit("Ⅷ") # ValueError: not a digit unicodedata.decimal("Ⅷ") # ValueError: not a decimal unicodedata.numeric("Ⅷ") # 8.0 unicodedata.digit("四") # ValueError: not a digit unicodedata.decimal("四") # ValueError: not a decimal unicodedata.numeric("四") # 4.0 #"〇","零","一","壱","二","弐","三","參","四","五","六","七","八","九","十","廿","卅","卌","百","千","萬","萬","億"
6.is其餘
4.存一個值or多個值:一個值
5.有序or無序:只要是有索引的,都是有序的,所以字符串是有序的。
name = 'nick' print(f'first:{id(name)}') name = 'nick handsome' print(f'second:{id(name)}')
first:4377100160 second:4377841264
6.可變or不可變:不可變數據類型