>>>a = "123" >>>print(type(a),a) ##能夠獲得該類型爲str >>>b = int(a) >>>print(type(b),b) ##能夠獲得該類型爲int,將字符串"123"轉換成了數字123 >>>num = "0011" >>>v = int(num, base = 16) >>>print(v) ## 按着16進制來處理
將一個數值按着或base類型的字符串轉換成整數,當有base以後,x必須爲str.base的取值範圍爲2~36.
>>>bin(37) '0b100101' >>>(37).bit_lenght() 6
常有功能:git
移除空白api
-lstrip(self, chars = None)ide
>>>s = " rany " >>>s.lstrip() >>>print(s) rany ##左邊的兩個空格沒有了,而右邊的兩個空格還在 >>>s = 'dgsnja' >>>s.lstrip(d) >>>print(s) gsnja ##表示去掉從左邊開始的,括號裏的值
-rstrip(self,chars = None)函數
從右邊開始,其餘同lstrip()spa
-strip(self, chars = None)code
去掉兩邊的,其餘同lstrip()orm
分割索引
-partition(self, sep)ip
分割成前、中、後是三個部分
>>> v = 'abcdabcdfhkdfk' >>> m = v.partition('d') >>> print(m) ('abc', 'd', 'abcdfhkdfk')
-rpartition(self, sep)
從右邊開始,其餘與partition()雷同
-split(self, sep=None, maxsplit=None)
以什麼爲基礎,最多分割幾回
('abc', 'd', 'abcdfhkdfk') >>> v = "abcabcdabcdefg" >>> m = v.split("c",3) >>> print(m) ['ab', 'ab', 'dab', 'defg'] ##注意:被做爲基礎的值,將會被遺棄
-rsplit(self, sep=None, maxsplit=None)
從右邊開始,其餘與split()雷同
-splitlines(self, keepends=False)
分割,可是隻保留行
>>> v = "I like your eyes\nI love you\nI think you are good" >>> m = v.splitlines() >>> print(m) ['I like your eyes', 'I love you', 'I think you are good']
長度
能夠用len()函數,獲得其長度,在Python 2裏面會獲得其字節長度,而Python 3中會獲得其字符長度
>>> v = "I love you" >>> print(len(v)) 10
索引
>>> v = "I love you" >>> print(v[0]) ##經過索引下標能夠獲得字符裏的值,其是從0開始計數 I
判斷 獲得的都是bool
-endswith(self, suffix, start = None, end = None)
以什麼結尾
>>>test = "I like to play games" >>>v = test.endswith("es") >>>print(v) True
-startswith(self, suffix, start = None, end = None)
以什麼開始,與endswith雷同
-isalnum(self)
判斷是否爲數值或者字母
-isalpha(self)
判斷是否爲漢字或者字母
-isdigit(self)
True: Unicode數字,byte數字(單字節),全角數字(雙字節)
False: 漢字數字 ,羅馬數字
Error: 無
-isdecimal(self)
True: Unicode數字,全角數字(雙字節)
False: 羅馬數字,漢字數字
Error: byte數字(單字節)
-isnumeric(self)
True: Unicode數字,全角數字(雙字節),羅馬數字,漢字數字
False: 無
Error: byte數字(單字節)
-isprintable(self)
判斷是否有\t製表符或者\n換行符,若是有就返回False
-isspace(self)
判斷是否所有爲空格
-istitle(self)
判斷是否爲標題,即每個字母都是大寫開頭
-islower(self)
判斷是否所有爲小寫
-issupper(self)
判斷是否所有爲大寫
查找
-count(self, sub, start = None, end = None)
查找子序列的個數
>>>test = "aLexalexr >>>v = test.count('ex',5,6) >>>print(v) 1
-find(self, sub, start = None, end = None)
尋找子序列位置,若是沒有找到,則返回「1」
>>>test = "L like your hair" >>>v = test.find('your') 7
-index(self, sub, start = None, end = None)
查找索引,同find()雷同,可是若是沒有找到,則報錯
切片
>>> v = "I love you" >>> print(v[0:5]) ##能夠取得必定範圍裏的字符,可是其顧頭不顧尾 I lov
變大小寫
-capitalize(self)
讓該字符串全部單詞的首字母變大寫
>>>test = "l like your eyes" >>>test.capitalize() >>>print(test) L Love Your Eyes
-lower(self)
讓字符串裏面全部字母變成小寫
>>>test = "L lIke Your Eyes" >>>test.lower() >>>print(test) l like your eyes
-casefold(self)
同lower,可是不一樣之處在於casefold的功能更強大,適用於更多種類的語言
>>>test = "L Like yOur eyEs" >>>test.casefold >>>print(test) l like your eyes
-swapcase(self)
大小寫相互轉換
>>> v = "I Love You" >>> m = v.swapcase() >>> print(m) i lOVE yOU
-isidentifier(self)
判斷是否爲標識符
>>> v = "False" >>> m = v.isidentifier() >>> print(m) True
填充替換
-center(self, width, fillchar = None)
設置總長度,並將內容居中
>>>test = 'yy' >>>v = test.center(6,"*") >>>print(v) **yy**
-ljust(self, width, fillchar = None)
從左邊開始填充,其餘與center雷同
-rjust(self, width, fillchar = None)
從右邊開始填充,其餘與center雷同
-zfill(self, width)
雷同於ljust(),可是隻填充0
>>>test = 'yy' >>>v = test.zfill(6,"*") >>>print(v) 0000yy
-join(self, iterable)
鏈接
>>>test = "loveyou" >>>v = '-'.join(test) >>>print(v) l-o-v-e-y-o-u
-replace(self, old, new, count=None)
把某字符轉換成指定的字符
>>> v = "I like you" >>> m = v.replace("like", "love") >>> print(m) I love you
其餘
-expandtabs(self, tabsize = None)
將tab轉換成空格,默認一個tab轉換成8個空格
>>>test = "username\temail\tpassword" >>>v = test.expandtabs(3) >>>print(v) username temail tpassword ##這些單詞之間都多了兩個空格
-format(*args, **kwargs)
格式化
>>>test = 'I am {name}, age {a}' >>>v = test.format(name = "Hermaeus", age = 19) >>>print(v) I an Hermaeus, age 19
-translate(self, table, deletechars=None)
轉換,須要提早作一個對應表,最後一個表示刪除字符集合
>>> v = "I like your eyes" >>> m = str.maketrans("your", "1234") ##先創建一個對應關係,兩個值必須等長 >>> new_v = v.translate(m) ##轉換 >>> print(new_v) I like 1234 e1es
循環
能夠經過for循環打印字符串
>>> v = "I love you" >>> for i in v: print(i) I l o v e y o u
能夠經過range()函數獲得一個連續的,或者遞增/遞減的取值範圍
>>> for i in range(1,10,2): print(i) 1 3 5 7 9