字符串常用函數
阿新 • • 發佈:2017-11-17
ngs inter containe comparing 默認 option ont pri als
1、count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
---取字符串當中 字符出現的次數,start,end 默認為空 表示字符從頭到尾。 start是從第幾個字符開始,第一字符位是0.
S.count(sub[, start[, end]]) -> int
例如:
i1 = "xuexipython"
print (i1.count(‘x‘))
----“xuexipython” 當中 字符‘x’ 出現的次數 為2次。
print (i1.count(‘x‘,1,20))
----“xuexipython” 當中 從u字符開始,字符‘x’ 出現的次數 為1次。
2、capitalize(self): # real signature unknown; restored from __doc__
"""
S.capitalize() -> string
---字符串首字母轉換為大寫
Return a copy of the string S with only its first character
capitalized.
例如:
i1 = "xuexipython"
print (i1.capitalize())
---結果為:“Xuexipython”
3、center(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""指定字符串長度 然後字符居中 默認用空格填充 fillchar = none ,也可以用指定字符來填充
S.center(width[, fillchar]) -> string
Return S centered in a string of length width. Padding is
done using the specified fill character (default is a space)
例如:
i1 = "xuexipython"
i2 = i1.center(19,‘#‘)
print (i2)
---####xuexipython#### ----用“#”來填充
4、upper(self): # real signature unknown; restored from __doc__
""" 把字符串轉換為大寫
S.upper() -> string
Return a copy of the string S converted to uppercase.
5、lower(self): # real signature unknown; restored from __doc__
"""把字符串轉換為小寫
S.lower() -> string
Return a copy of the string S converted to lowercase.
6、index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
""" 查找字符串中,指定字符開始的位置,star,end 默認值為空,在整個字符串中進行查找,也可指定字符中起始位置。
功能類似於:find
區別在於index 在查找時,查詢不到結果時報錯,ValueError: substring not found
find 在查找時,查詢不到結果時不會報錯,‘-1’
S.index(sub [,start [,end]]) -> int
Like S.find() but raise ValueError when the substring is not found.
例如:
i1 = "xuexipython"
print (i1.index(‘xi‘))
----‘xi’字符在 "xuexipython"中在第幾位置,3
7、 find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""查找字符串中,指定字符開始的位置,star,end 默認值為空,在整個字符串中進行查找,也可指定字符中起始位置。
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.
8、endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
""" endswith() 方法用於判斷字符串是否以指定後綴結尾,如果以指定後綴結尾返回True,否則返回False。可選參數"start"與"end"為檢索字符串的開始與結束位置。
S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
例如:
i1 = "xuexipython"
print (i1.endswith(‘n‘))
返回值:true
print (i1.endswith(‘i‘,0,5))
返回值:true
9、format(self, *args, **kwargs): # known special case of str.format
"""格式化 只能從0開始
S.format(*args, **kwargs) -> string
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces (‘{‘ and ‘}‘).
例如:
i1 = "xuexipython,{0},{1}"
print (i1.format(‘age‘,‘tt‘))
返回值:xuexipython,age,tt
10、isalnum(self): # real signature unknown; restored from __doc__
"""是判斷 self 是否有字符或數字
S.isalnum() -> bool
Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.
例如:
i1 = "xuexipython"
i2 = "test230"
i3 = " "
print (i1.isalnum())
print (i2.isalnum())
print (i3.isalnum())
返回值:
True
True
False
11、isalpha(self): # real signature unknown; restored from __doc__
"""判斷self中的值是否全為字符
S.isalpha() -> bool
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
例如:
i1 = "xuexipython"
i2 = "test230"
i3 = " "
print (i1.isalpha())
print (i2.isalpha())
print (i3.isalpha())
返回值:
True
False
False
12、isspace(self): # real signature unknown; restored from __doc__
""" 判斷self字符串是不是都為空格,
S.isspace() -> bool
Return True if all characters in S are whitespace
and there is at least one character in S, False otherwise.
例如:
i1 = "xuexipython "
i2 = " test230"
i3 = " "
print (i1.isspace())
print (i2.isspace())
print (i3.isspace())
返回值:
False
False
True
13、 lstrip str.lstrip([chars])
lstrip() 方法用於截掉字符串左邊的空格或指定字符,或指定的字符串
例如:
i1 = "xuexipython"
print (i1.lstrip(‘x‘))
返回:uexipython
14、 rstrip str.rstrip([chars])
lstrip() 方法用於截掉字符串左邊的空格或指定字符,或指定的字符串
例如:
i1 = "xuexipython"
print (i1.rstrip(‘n‘))
返回:xuexipytho
15、strip str.strip([chars])
strip() 方法用於截掉字符串兩邊的空格。
16、ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""字符靠左對齊,不夠補齊,默認為空格
S.ljust(width[, fillchar]) -> string
Return S left-justified in a string of length width. Padding is
done using the specified fill character (default is a space).
例如:
i1 = "xuexipython"
print (i1.ljust(20,‘#‘))
返回:xuexipython#########
17、rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__
"""字符靠右對齊,不夠補齊,默認為空格,同ljust正好相反
S.rjust(width[, fillchar]) -> string
Return S right-justified in a string of length width. Padding is
done using the specified fill character (default is a space)
字符串常用函數