字符串介紹
1、字符串在內存中的存儲;
2、字符串相加;
3、字符串的格式化;
In [1]: a = 100 In [2]: a Out[2]: 100 #100<255,在堆內存下占用了一個字節,一個字節8位,可以存放的數值最大為255。 In [3]: b = "100" In [4]: b Out[4]: '100' #對應ASCII碼存放,一個字節存放任何的一個字符,因此字符串100對應3個字符即占用3個字節。字符串占用空間大。 In [5]: type(a) Out[5]: int In [6]: type(b) Out[6]: str
In [4]: b = '100' In [7]: c = "200" In [8]: b + c Out[8]: '100200'
In [9]: d = "b+c=%s" %(b+c) In [10]: d Out[10]: 'b+c=100200'
In [11]: e="abcefg%sgfgfgfg"%b In [12]: e Out[12]: 'abcefg100gfgfgfg'
下標索引:就是編號,通過這個編號能找到相應的存儲空間。
字符串中下標的使用:字符串實際上就是字符的數組,所以也支持下標索引。
In [27]: name Out[27]: 'abcdefghijk' In [28]: name[0] Out[28]: 'a' In [29]: name[-1] Out[29]: 'k' In [30]: len(name) Out[30]: 11 In [32]: name[len(name)-1] Out[32]: 'k' In [33]: name[-10] Out[33]: 'b' In [34]: name[10] Out[34]: 'k'
切片:是指對操作對象截取其中一部分的操作,字符串、列表、元組都支持切片操作
切片的語法:【起始:結束:步長】
步長是下標變化的規律,默認為1
注意:選取的區間屬於左閉右開型(包頭不包尾)
In [1]: name="abdfsdfsdf" In [2]: name[1:4] Out[2]: 'bdf' In [3]: name[:] Out[3]: 'abdfsdfsdf' In [4]: name[::2] Out[4]: 'adsfd' In [5]: name[0:] Out[5]: 'abdfsdfsdf' In [6]: name[:-1] Out[6]: 'abdfsdfsd'
將輸入的字符串倒序輸出
1 s=input("請輸入一個字符串:") 2 i=len(s) 3 while i > 0 : 4 i -= 1 5 print("%s"%(s[i]),end="") 6 print("") [root@localhost python]# python3 20.py 請輸入一個字符串:love evol [root@localhost python]#
通過切片實現字符串倒序輸出
In [1]: name="asdsadsds" In [2]: name Out[2]: 'asdsadsds' In [3]: name[-1::-1] Out[3]: 'sdsdasdsa'
字符串操作:函數調用的操作
In [9]: name="love" In [10]: name. //按Tab鍵顯示字符串的方法 name.capitalize name.encode name.format name.isalpha name.islower name.istitle name.lower name.casefold name.endswith name.format_map name.isdecimal name.isnumeric name.isupper name.lstrip name.center name.expandtabs name.index name.isdigit name.isprintable name.join name.maketrans > name.count name.find name.isalnum name.isidentifier name.isspace name.ljust name.partition
查找方法:find()和index()
In [10]: my_str="hello world zyj sl everyone in the world" In [11]: my_str.find("zyj") Out[11]: 12 #目標字符串的第一個字符所在的下標位 In [12]: my_str.find("lw") Out[12]: -1 #找不到時返回-1,當返回小於0時,即可判斷沒有找到。 In [13]: my_str.find("world") Out[13]: 6 #默認從左邊開始查找,並輸出第一個找到的位置 In [14]: my_str.rfind("world") Out[14]: 35 #.rfind方法從右邊開始查找 In [15]: my_str.index("world") Out[15]: 6 #index()默認從左邊查找 In [17]: my_str.rindex("world") Out[17]: 35 #rindex()從右邊查找 In [16]: my_str.index("lw") --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-16-a951699b322b> in <module>() ----> 1 my_str.index("lw") ValueError: substring not found #查找不到時報錯,與find()的區別
計數及替換方法:count()和replace()
In [18]: my_str.count("world")#計算目標元素出現的次數。 Out[18]: 2 In [19]: my_str.count("lw") Out[19]: 0 In [20]: my_str.count("zyj") Out[20]: 1 In [21]: my_str.replace("hello","Hi") Out[21]: 'Hi world zyj sl everyone in the world' In [22]: In [22]: my_str Out[22]: 'hello world zyj sl everyone in the world' #原字符串沒有改變 In [23]: my_str.replace("world","city")#默認替換所有查找到的目標元素 Out[23]: 'hello city zyj sl everyone in the city' In [24]: my_str.replace("world","city",1) #將查找到的第一個替換,指定count,則替換不超過count次。 Out[24]: 'hello city zyj sl everyone in the world'
分隔:split(),以str為分隔符切片mystr,可以指定最多分割成多少個段。
In [25]: my_str Out[25]: 'hello world zyj sl everyone in the world' In [26]: my_str.split(" ") #以空格作為分隔符 Out[26]: ['hello', 'world', 'zyj', 'sl', 'everyone', 'in', 'the', 'world'] In [29]: my_str.split() #默認以空格作為分隔符,返回的是列表 Out[29]: ['hello', 'world', 'zyj', 'sl', 'everyone', 'in', 'the', 'world'] In [27]: my_str.split(" ",2) #指定最多包含兩個分隔符 Out[27]: ['hello', 'world', 'zyj sl everyone in the world'] In [28]: my_str.split("zyj") Out[28]: ['hello world ', ' sl everyone in the world'] #結果中不顯示“zyj”,把它當作分隔符了。 In [30]: my_str.partition("zyj") Out[30]: ('hello world ', 'zyj', ' sl everyone in the world') #將本身也作為一個元素,與split()的區別,包含隔開符。
分隔:partition()注意與split()的區別。
In [25]: my_str Out[25]: 'hello world zyj sl everyone in the world' In [36]: my_str.partition() #不支持這種寫法,必須指定分隔標志。 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-36-c027286912d6> in <module>() ----> 1 my_str.partition() TypeError: partition() takes exactly one argument (0 given) In [37]: my_str.partition(" ") #以空格作為分隔符,遇到的第一個空格作為分隔符,自身也是分隔元素 Out[37]: ('hello', ' ', 'world zyj sl everyone in the world') In [38]: my_str.partition(' ',3) #不支持設置最大分隔數 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-38-86d354aca67f> in <module>() ----> 1 my_str.partition(' ',3) TypeError: partition() takes exactly one argument (2 given)
capitalize():把字符串的第一個字符大寫。
title():將每個字符串的第一個單詞大寫
In [41]: my_str.capitalize() Out[41]: 'Hello world zyj sl everyone in the world' In [42]: my_str.title() Out[42]: 'Hello World Zyj Sl Everyone In The World'
startswith():檢查字符串以什么開頭,返回布爾值:True或False
endswith():檢查字符串以什么開頭,返回布爾值:True或False
In [43]: my_str.startswith("hello") Out[43]: True In [44]: my_str.startswith("Hello") Out[44]: False In [45]: my_str.endswith("wor") Out[45]: False In [46]: my_str.endswith("world") Out[46]: True #使用場景 In [50]: file_name="XXX.txt" In [52]: if file_name.endswith(".txt"): ...: print("文本文件") ...: 文本文件 In [53]:
lower():將字符串中的所有大寫字符變為小寫;
upper():將字符串中的所有小寫字母變為大寫;
In [57]: my_str Out[57]: 'Hello WEWER dsfsdf dfsdf' In [58]: my_str.upper() Out[58]: 'HELLO WEWER DSFSDF DFSDF' In [59]: my_str.lower() Out[59]: 'hello wewer dsfsdf dfsdf' #應用場景,驗證碼不區分大小寫,用戶輸入的進行轉換。 In [61]: str="ABC" #目標字符串 In [62]: user_str="Abc" #用戶輸入字符串 In [63]: user_str.upper() #對用戶輸入的進行轉換后比較 Out[63]: 'ABC'
排列對齊操作:ljust() rjust() center()
In [66]: name Out[66]: 'ddsfsdfsdfdfdsf' In [67]: name.ljust(50) #50代表長度,返回一個使用空格填充至長度的新字符串 Out[67]: 'ddsfsdfsdfdfdsf ' In [68]: In [68]: name.rjust(50) Out[68]: ' ddsfsdfsdfdfdsf' In [69]: name.center(50) Out[69]: ' ddsfsdfsdfdfdsf ' In [70]:
刪除空白字符:lstrip() rstrip() strip()
In [70]: name=" abc " In [71]: name.lstrip() #刪除左邊的空白符 Out[71]: 'abc ' In [72]: name.rstrip() #刪除右邊的空白符 Out[72]: ' abc' In [73]: name.strip() #刪除左右兩側空白符 strip()=trim() java中使用trim()
Out[73]: 'abc' In [74]:
換行符隔開:splitlines()
In [76]: lines="anbc\ndfsdfdf\ndfsdfdf\ndfdf" In [77]: lines.splitlines() Out[77]: ['anbc', 'dfsdfdf', 'dfsdfdf', 'dfdf'] In [78]: lines.split("\n") Out[78]: ['anbc', 'dfsdfdf', 'dfsdfdf', 'dfdf'] In [79]:
字符串中只包含數字、字母、數字或字母、空格的操作,返回布爾值 isalnum() isalpha() isdigit() isspace()
In [1]: test="122323dsfdfsdfsdf" In [2]: test.isalnum() Out[2]: True In [3]: test.isalpha() Out[3]: False In [4]: test.isdigit() Out[4]: False In [5]: test.isspace() Out[5]: False In [6]: In [6]: test1=" " In [7]: test1.isspace() Out[7]: True
mystr.join(list):mystr中每個字符后面插入list的每個元素后面,構造出一個新的字符串
應用:將列表轉換為字符串
In [8]: names=["100","200","300"] In [9]: names Out[9]: ['100', '200', '300'] In [10]: "".join(names) Out[10]: '100200300' In [11]: "-".join(names) Out[11]: '100-200-300'
查看方法的幫助文檔:
In [16]: test="122323dsfdfsdfsdf"
In [14]: help(test.find) find(...) method of builtins.str instance S.find(sub[, start[, end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.
(面試題)給定一個字符串,返回使用空格或者\r \t分割后的倒數第二個字串。
In [1]: name="hel eewrje\twrjwer\twerwer ew\nrewr" In [6]: help(name.split) In [7]: name.split() #會將字符串中的空格以及特殊意義的符號作為分隔符。 Out[7]: ['hel', 'eewrje', 'wrjwer', 'werwer', 'ew', 'rewr'] In [8]: name.split()[-2] #返回的是列表。列表支持下標操作。 Out[8]: 'ew'