Python3

mac2022-06-30  73

name = qiuwenbin

name = str('qiuwenbin')----按住ctrl点击方法,通过pycharm能成功定位方法位置

name = str('qiuwenbin')result = name.__contains__('qu')result = "qiu" in name

  上面的两种方式一样print(result)

常用的方法:

  1.首字母大写

def capitalize(self): # real signature unknown; restored from __doc__ """ S.capitalize() -> str Return a capitalized version of S, i.e. make the first character have upper case and the rest lower case. """ return "" View Code

  2.首字母边小写

def casefold(self): # real signature unknown; restored from __doc__ """ S.casefold() -> str Return a version of S suitable for caseless comparisons. """ return "" View Code

  3.居中显示,并且自定义两边字符串

1 name = str('qiuwenbin') 2 result = name.center(20,"*") 3 print(result) View Code

  4.统计数量

name = str('qiuwenbinasdadasdqwqqqweqq qweqwe')result = name.count("q",0,10)print(result)

def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ """ S.count(sub[, start[, end]]) -> int Return the number of non-overlapping occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation. """ return 0 View Code

  5.判断一个对象是以什么结尾

name = "qiuwenbin"result = name.endswith("n")print(result)

def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__ """ 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. """ return False View Code

  6.tab(/t)变成空格

name = "qiuwen\tbin"result = name.expandtabs()print(result)print(len(result))

def expandtabs(self, tabsize=8): # real signature unknown; restored from __doc__ """ S.expandtabs(tabsize=8) -> str Return a copy of S where all tab characters are expanded using spaces. If tabsize is not given, a tab size of 8 characters is assumed. """ return "" View Code

   7.找出字符串所在对象中的位置

name = "qiuwen\tbin"result = name.find("n")print(result)

    ---与index的方法类似,但是find如果找不到返回值是“-1”,index没有找到则返回报错

def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__ """ 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. """ return 0 View Code

  8.返回一个格式化的版本

name = "qiu {0} wen{1} bin{2}"result = name.format('wang','xiao','qin')print(result)

def format(self, *args, **kwargs): # known special case of str.format """ S.format(*args, **kwargs) -> str Return a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces ('{' and '}'). """ pass View Code

  9.列表拼接

lsit = ['s','b','x','x','L']result = "".join(lsit)print(result)

&

lsit = ['s','b','x','x','L']result = str("$").join(lsit)print(result)

def join(self, iterable): # real signature unknown; restored from __doc__ """ S.join(iterable) -> str Return a string which is the concatenation of the strings in the iterable. The separator between elements is S. """ return "" View Code

  10.分隔符,保留分割关键字。

name = "qiuwenbinlovewangxiaoqin"result = name.partition("love")print(result)

def partition(self, sep): # real signature unknown; restored from __doc__ """ S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part before it, the separator itself, and the part after it. If the separator is not found, return S and two empty strings. """ pass View Code

  11.替换

name = "qiuwenbinlovewangxiaoqin"result = name.replace("a","Y",2)print(result)

def replace(self, old, new, count=None): # real signature unknown; restored from __doc__ """ S.replace(old, new[, count]) -> str Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. """ return "" View Code

  12.splitlines()方法

name= '''112233'''result = name.splitlines()print(result)

def splitlines(self, keepends=None): # real signature unknown; restored from __doc__ """ S.splitlines([keepends]) -> list of strings Return a list of the lines in S, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true. """ return [] View Code

  13.换行符处理,\n是用于str内部的

>>> print(b'abc\ndef') b'abc\ndef' >>> print('abc\ndef') abc def >>> type(b'') <class 'bytes'> >>> type('') <class 'str'>

 

转载于:https://www.cnblogs.com/Bingo0-python/p/6837537.html

最新回复(0)