• Python字符串部分源码分析


    1 def capitalize(self): # real signature unknown; restored from __doc__
    2         """
    3         S.capitalize() -> str
    4         
    5         Return a capitalized version of S, i.e. make the first character
    6         have upper case and the rest lower case.
    7         """
    8         return ""
    1 #练习1、首字母大写
    2 li = str("sdfsdagasdf23s5435436dsfdsg")
    3 print(li)
    4 aa = li.capitalize()       #字符串首字母变大写
    5 print(aa)
    6 执行结果: 7 sdfsdagasdf23s5435436dsfdsg 8 Sdfsdagasdf23s5435436dsfdsg
    1 def casefold(self): # real signature unknown; restored from __doc__
    2         """
    3         S.casefold() -> str
    4         
    5         Return a version of S suitable for caseless comparisons.
    6         """
    7         return ""
    1 #练习2、所有大写变小写
    2 li = str("sdfsdaGaSdf23s5435436DsfDsG")
    3 print(li)
    4 aa = li.casefold()       #把字符串里所有大写字母变小写
    5 print(aa)
    6 执行结果: 7 sdfsdaGaSdf23s5435436DsfDsG 8 sdfsdagasdf23s5435436dsfdsg
    1 def center(self, width, fillchar=None): # real signature unknown; restored from __doc__
    2         """
    3         S.center(width[, fillchar]) -> str
    4         
    5         Return S centered in a string of length width. Padding is
    6         done using the specified fill character (default is a space)
    7         """
    8         return ""
    #练习3、将字符串置于长度长的空间的中间,两边填充其他
    li = str("sdfGDS324D")
    aa = li.center(20,"*")    #字符串li总长度10,把字符串li置于20个长度的中间,两边空余的地方用“*”填充
    print(li)
    print(aa)
    #执行结果:
    sdfGDS324D
    *****sdfGDS324D*****
    1 def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    2         """
    3         S.count(sub[, start[, end]]) -> int
    4         
    5         Return the number of non-overlapping occurrences of substring sub in
    6         string S[start:end].  Optional arguments start and end are
    7         interpreted as in slice notation.
    8         """
    9         return 0
     1 #练习4、计算指定位置段,指定字符出现的次数
     2 li = str("sdfGDS324DsdgfeGRAfsdf232")
     3 aa = li.count("s")    #计算指定字符在字符串中出现的次数
     4 print(aa)
     5 bb = li.count("s",0,10)    #计算指定位置段的字符出现次数,0<=x<10,即顾前不顾后
     6 print(bb)
     7 
     8 #执行结果:
     9 3
    10 1
     1 def encode(self, encoding='utf-8', errors='strict'): # real signature unknown; restored from __doc__
     2         """
     3         S.encode(encoding='utf-8', errors='strict') -> bytes
     4         
     5         Encode S using the codec registered for encoding. Default encoding
     6         is 'utf-8'. errors may be given to set a different error
     7         handling scheme. Default is 'strict' meaning that encoding errors raise
     8         a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
     9         'xmlcharrefreplace' as well as any other name registered with
    10         codecs.register_error that can handle UnicodeEncodeErrors.
    11         """
    12         return b""
     1 #练习5、编码转换
     2 li = str("sdfGDS324DsdgfeGRAfsdf232")
     3 aa = li.encode("gbk")   #这里源编码是utf-8,转换为其他编码gbk
     4 print(li)
     5 print(aa)
     6 bb = aa.decode()      #其他编码转换回默认的utf-8编码
     7 print(bb)
     8 
     9 执行结果:
    10 sdfGDS324DsdgfeGRAfsdf232
    11 b'sdfGDS324DsdgfeGRAfsdf232'
    12 sdfGDS324DsdgfeGRAfsdf232
     1 def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
     2         """
     3         S.endswith(suffix[, start[, end]]) -> bool
     4         
     5         Return True if S ends with the specified suffix, False otherwise.
     6         With optional start, test S beginning at that position.
     7         With optional end, stop comparing S at that position.
     8         suffix can also be a tuple of strings to try.
     9         """
    10         return False
     1 #练习6、字符串结尾是否正确
     2 li = str("sdfGDS324DsdgfeGRAfsdf232")
     3 aa = li.endswith("kkk")               #当然也有验证字符串前头是否正确,startswith
     4 bb = li.endswith("232")
     5 print(aa)
     6 print(bb)
     7 
     8 #执行结果:
     9 False
    10 True
    1 def expandtabs(self, tabsize=8): # real signature unknown; restored from __doc__
    2         """
    3         S.expandtabs(tabsize=8) -> str
    4         
    5         Return a copy of S where all tab characters are expanded using spaces.
    6         If tabsize is not given, a tab size of 8 characters is assumed.
    7         """
    8         return ""
    1 #练习7、将tab转换为8个空格
    2 li = str("sdfGDS324	DsdgfeGRAfsdf232")
    3 aa = li.expandtabs()    #将tab转换为8个空格
    4 print(aa)
    5 
    6 #执行结果:
    7 sdfGDS324       DsdgfeGRAfsdf232
     1 def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
     2         """
     3         S.find(sub[, start[, end]]) -> int
     4         
     5         Return the lowest index in S where substring sub is found,
     6         such that sub is contained within S[start:end].  Optional
     7         arguments start and end are interpreted as in slice notation.
     8         
     9         Return -1 on failure.
    10         """
    11         return 0
     1 #练习8、寻找指定字母位置
     2 li = str("sdfGDS324DsdgfeGRAfsdf232")
     3 aa = li.find("G")    #查找指定字母位置,从左往右找
     4 bb = li.rfind("G")    #查找指定字母位置,从右往左找
     5 print(aa)
     6 print(bb)
     7 
     8 #执行结果:
     9 3
    10 15
    1 def format(self, *args, **kwargs): # known special case of str.format
    2         """
    3         S.format(*args, **kwargs) -> str
    4         
    5         Return a formatted version of S, using substitutions from args and kwargs.
    6         The substitutions are identified by braces ('{' and '}').
    7         """
    8         pass
    1 #练习9、字符串格式化
    2 li = str("sdfGDS324{0}
    Dsdgfe{1}
    GRAfsdf232")
    3 aa = li.format("-----","*****")      #字符串格式化,可以优化输出结果
    4 print(aa)
    5 
    6 #执行结果:
    7 sdfGDS324-----
    8 Dsdgfe*****
    9 GRAfsdf232
    1 def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    2         """
    3         S.index(sub[, start[, end]]) -> int
    4         
    5         Like S.find() but raise ValueError when the substring is not found.
    6         """
    7         return 0
     1 #练习10、查找字符索引
     2 li = str("sdfGDS324DsdgfeGRAfsdf232")
     3 aa = li.index("sd",10,20)    #查找指定位置段指定字符串的索引,注意:只输出第一个
     4 bb = li.index("sd")          #默认寻找全部第一个
     5 print(aa)
     6 print(bb)
     7 
     8 #执行结果:
     9 10
    10 0
    1 def isalnum(self): # real signature unknown; restored from __doc__
    2         """
    3         S.isalnum() -> bool
    4         
    5         Return True if all characters in S are alphanumeric
    6         and there is at least one character in S, False otherwise.
    7         """
    8         return False
     1 #练习11、判断字符串是否为纯字母和数字
     2 li = str("sdfGDS324DsdgfeGRAfsdf232")
     3 aa = li.isalnum()                              #纯字母和数字则为真
     4 li11 = str("!@#$%^sdgsdg235")      #否则为假
     5 bb = li11.isalnum()
     6 print(aa)
     7 print(bb)
     8 
     9 #执行结果:
    10 True
    11 False
    1 def isalpha(self): # real signature unknown; restored from __doc__
    2         """
    3         S.isalpha() -> bool
    4         
    5         Return True if all characters in S are alphabetic
    6         and there is at least one character in S, False otherwise.
    7         """
    8         return False
     1 #练习12、判断字符串是否纯为字母
     2 li = str("sdfGDS324DsdgfeGRAfsdf232")
     3 aa = li.isalpha()               #判断字符串是否纯为字母,不是则为加
     4 li11 = str("fsdgahsdfh")    #否则为真
     5 bb = li11.isalpha()
     6 print(aa)
     7 print(bb)
     8 
     9 #执行结果:
    10 False
    11 True
    1 def isdigit(self): # real signature unknown; restored from __doc__
    2         """
    3         S.isdigit() -> bool
    4         
    5         Return True if all characters in S are digits
    6         and there is at least one character in S, False otherwise.
    7         """
    8         return False
     1 #练习13、判断字符串是否纯为数字
     2 li = str("sdfGDS324DsdgfeGRAfsdf232")
     3 aa = li.isdigit()                 #判断字符串是否纯为数字,不是则为假
     4 li11 = str("2142353156")    #否则为真
     5 bb = li11.isdigit()
     6 print(aa)
     7 print(bb)
     8 
     9 #执行结果:
    10 False
    11 True
    1 def islower(self): # real signature unknown; restored from __doc__
    2         """
    3         S.islower() -> bool
    4         
    5         Return True if all cased characters in S are lowercase and there is
    6         at least one cased character in S, False otherwise.
    7         """
    8         return False
     1 #练习14、判断字符串是否纯为小写字母
     2 li = str("sdfGDS324DsdgfeGRAfsdf232")
     3 aa = li.islower()           #字符串不是纯小写字母,则为假
     4 li11 = str("aaaaaaaa")    #否则为真
     5 bb = li11.islower()
     6 print(aa)
     7 print(bb)
     8 
     9 #执行结果:
    10 False
    11 True
    1 def join(self, iterable): # real signature unknown; restored from __doc__
    2         """
    3         S.join(iterable) -> str
    4         
    5         Return a string which is the concatenation of the strings in the
    6         iterable.  The separator between elements is S.
    7         """
    8         return ""
    #练习15、一个字符串加入另外一个字符串
    li = str("sdfGDS32")
    aa = "***"
    bb = li.join(aa)     #字符串li加入到字符串aa中的每个字符中间
    cc = aa.join(li)
    print(bb)
    print(cc)
    
    #执行结果:
    *sdfGDS32*sdfGDS32*
    s***d***f***G***D***S***3***2
    1 def ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__
    2         """
    3         S.ljust(width[, fillchar]) -> str
    4         
    5         Return S left-justified in a Unicode string of length width. Padding is
    6         done using the specified fill character (default is a space).
    7         """
    8         return ""
     1 #练习16、字符串左对齐
     2 li = str("sdfGDS32")
     3 aa = li.ljust(20,"*")     #指定空间左对齐,不足的可以填充其他,可用于优化显示
     4 bb = li.ljust(20)         #当然也有右对齐rjust,用法一样
     5 print(aa)
     6 print(bb)
     7 
     8 #执行结果
     9 sdfGDS32************
    10 sdfGDS32  
    1 def lower(self): # real signature unknown; restored from __doc__
    2         """
    3         S.lower() -> str
    4         
    5         Return a copy of the string S converted to lowercase.
    6         """
    7         return ""
    1 #练习17、把字符串的大写字母变为小写
    2 li = str("sdfGDS32")
    3 aa = li.lower()    #将字符串所有的大写字母变为小写
    4 print(li)
    5 print(aa)
    6 
    7 #执行结果:
    8 sdfGDS32
    9 sdfgds32
    1 def lstrip(self, chars=None): # real signature unknown; restored from __doc__
    2         """
    3         S.lstrip([chars]) -> str
    4         
    5         Return a copy of the string S with leading whitespace removed.
    6         If chars is given and not None, remove characters in chars instead.
    7         """
    8         return ""
    1 #练习18、移除左侧空白
    2 li = str("   sdfGDS32")
    3 aa = li.lstrip()    #移除字符串左侧空白,也有移除右侧空白rstrip,以及移除两端空白strip()
    4 print(li)
    5 print(aa)
    6 
    7 #执行结果:
    8    sdfGDS32
    9 sdfGDS32
    1 def partition(self, sep): # real signature unknown; restored from __doc__
    2         """
    3         S.partition(sep) -> (head, sep, tail)
    4         
    5         Search for the separator sep in S, and return the part before it,
    6         the separator itself, and the part after it.  If the separator is not
    7         found, return S and two empty strings.
    8         """
    9         pass
     1 #练习19、分割字符串
     2 li = str("sdfGDS32")
     3 aa = li.partition("G")    #分割,指定字符为中间部分,前面和后面2部分
     4 print(aa)
     5 bb = li.partition("*")    #如果指定字符不存在,在字符串后加空字符
     6 print(bb)
     7 
     8 #执行结果:
     9 ('sdf', 'G', 'DS32')
    10 ('sdfGDS32', '', '')
    1 def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
    2         """
    3         S.replace(old, new[, count]) -> str
    4         
    5         Return a copy of S with all occurrences of substring
    6         old replaced by new.  If the optional argument count is
    7         given, only the first count occurrences are replaced.
    8         """
    9         return ""
    1 #练习20、替换
    2 li = str("sdfGDS32sdyyer234sddfdg")
    3 aa = li.replace("sd","**",2)  # 替换,默认替换所有,加数字则替换前几个
    4 print(aa)
    5 
    6 #执行结果:
    7 **fGDS32**yyer234sddfdg
     1 def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
     2         """
     3         S.split(sep=None, maxsplit=-1) -> list of strings
     4         
     5         Return a list of the words in S, using sep as the
     6         delimiter string.  If maxsplit is given, at most maxsplit
     7         splits are done. If sep is not specified or is None, any
     8         whitespace string is a separator and empty strings are
     9         removed from the result.
    10         """
    11         return []
     1 #练习21、分割
     2 li = str("sdfGDS32sdyyer234sddfdg")
     3 aa = li.split("sd")  # 默认以指定字符为标志分割所有
     4 bb = li.split("sd",2)   #指定数字(最多分割几次)
     5 print(aa)
     6 print(bb)
     7 
     8 #执行结果:
     9 ['', 'fGDS32', 'yyer234', 'dfdg']
    10 ['', 'fGDS32', 'yyer234sddfdg']
    1 def splitlines(self, keepends=None): # real signature unknown; restored from __doc__
    2         """
    3         S.splitlines([keepends]) -> list of strings
    4         
    5         Return a list of the lines in S, breaking at line boundaries.
    6         Line breaks are not included in the resulting list unless keepends
    7         is given and true.
    8         """
    9         return []
    1 #练习22、根据换行分割
    2 li = str("sdfGDS32
    sdyyer
    234sddfdg")
    3 aa = li.splitlines()     #根据换行分割
    4 print(aa)
    5 
    6 #执行结果:
    7 ['sdfGDS32', 'sdyyer', '234sddfdg']
    1 def swapcase(self): # real signature unknown; restored from __doc__
    2         """
    3         S.swapcase() -> str
    4         
    5         Return a copy of S with uppercase characters converted to lowercase
    6         and vice versa.
    7         """
    8         return ""
    1 #练习23、大写转换为小写,小写转换为大写
    2 li = str("sdfGDS32sdyyer234sddfdg")
    3 aa = li.swapcase()     #大写转换为小写,小写转换为大写
    4 print(li)
    5 print(aa)
    6 
    7 #执行结果:
    8 sdfGDS32sdyyer234sddfdg
    9 SDFgds32SDYYER234SDDFDG
     1 def translate(self, table): # real signature unknown; restored from __doc__
     2         """
     3         S.translate(table) -> str
     4         
     5         Return a copy of the string S, where all characters have been mapped
     6         through the given translation table, which must be a mapping of
     7         Unicode ordinals to Unicode ordinals, strings, or None.
     8         Unmapped characters are left untouched. Characters mapped to None
     9         are deleted.
    10         """
    11         return ""
     1 def maketrans(self, *args, **kwargs): # real signature unknown
     2         """
     3         Return a translation table usable for str.translate().
     4         
     5         If there is only one argument, it must be a dictionary mapping Unicode
     6         ordinals (integers) or characters to Unicode ordinals, strings or None.
     7         Character keys will be then converted to ordinals.
     8         If there are two arguments, they must be strings of equal length, and
     9         in the resulting dictionary, each character in x will be mapped to the
    10         character at the same position in y. If there is a third argument, it
    11         must be a string, whose characters will be mapped to None in the result.
    12         """
    13         pass
     1 #练习24、转换,先做一个对应表,然后对换
     2 st = ""
     3 intab = "aeiou"
     4 outtab = "12345"
     5 table = st.maketrans(intab,outtab)    #需要结合maketrans使用,对应表
     6 astr = "aeiou-777777-12345"
     7 bstr = astr.translate(table)        #转换
     8 print(bstr)
     9 
    10 执行结果:
    11 12345-777777-12345
    1 def zfill(self, width): # real signature unknown; restored from __doc__
    2         """
    3         S.zfill(width) -> str
    4         
    5         Pad a numeric string S with zeros on the left, to fill a field
    6         of the specified width. The string S is never truncated.
    7         """
    8         return ""
    1 #练习25、返回指定字符串长度,长度超过字符串则以“0”填充
    2 li = str("213dsf3")
    3 aa = li.zfill(20)
    4 print(aa)
    5 
    6 #执行结果:
    7 0000000000000213dsf3
  • 相关阅读:
    Map
    input输入框文字提示IE兼容
    Linux下实现获取远程机器文件
    ssl_error_rx_record_too_long
    Linux下访问网站
    Linux安装Jdk1.7
    bootstrap左右圆角按钮-适配手机页面
    jQuery使用load方法加载其他文档内容
    Js操作DOM小练习_01
    BootstrapValidator验证表单用法
  • 原文地址:https://www.cnblogs.com/repo/p/5415205.html
Copyright © 2020-2023  润新知