python字符串的方法

python字符串的方法

############7个基本方法############

1:join

 1 def join(self, ab=None, pq=None, rs=None): # real signature unknown; restored from __doc__
 2     """
 3     Concatenate any number of strings.
 4     
 5     The string whose method is called is inserted in between each given string.
 6     The result is returned as a new string.
 7     
 8     Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
 9     """
10     可以向字符串插入 标识符 标点 字母等连接
test=("qweqweqw")
v="_".join(test)
print(v)


q_w_e_q_w_e_q_w




test=(['as','df','as'])
v="_".join(test)
print(v)



as_df_as

  

2:split(正则)

 1 split(self, *args, **kwargs): # real signature unknown
 2     """
 3     Return a list of the words in the string, using sep as the delimiter string.
 4     
 5       sep
 6         The delimiter according which to split the string.
 7         None (the default value) means split according to any whitespace,
 8         and discard empty strings from the result.
 9       maxsplit
10         Maximum number of splits to do.
11         -1 (the default value) means no limit.
12     """
13 全分割 从第一个逐一扫描字符串寻找所选字符 从字符串中找到该字符位置开始分割 (所选字符消失)
test=("asdfasdfasdfsadfsdf")
v=test.split("f")
print(v)



['asd', 'asd', 'asd', 'sad', 'sd', '']



test=("asdfasdfasdf")
v=test.split("s",2,)
print(v)



['a', 'dfa', 'dfasdf']

  




3:find

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
从左向右查找所定义的字符 显示首个该字符的索引(下标);find(字符,开始索引,结束索引)
test=("asdfasdfasdfsadfsdf")
v=test.find("f")
print(v)


3



test=("asdfasdfasdfsadfsdf")
v=test.find("f",5,15)
print(v)


7

  

4:strip

strip(self, *args, **kwargs): # real signature unknown
    """
    Return a copy of the string with leading and trailing whitespace remove.
    
    If chars is given and not None, remove characters in chars instead.
    """
清除字符串两边的空格
test=("  asdfasdfas   dfsadfsdf   ")
v=test.strip()
print(v)


asdfasdfas    dfsadfsdf

  

5:upper

upper(self, *args, **kwargs): # real signature unknown
    """ Return a copy of the string converted to uppercase. """
字符串全大写
test=("asdfasdfasdfsadfsdf")
v=test.upper()
print(v)



ASDFASDFASDFSADFSDF

  

6:lower

lower(self, *args, **kwargs): # real signature unknown
    """ Return a copy of the string converted to lowercase. """
    pass
字符串全小写
test=("ASDFASDFASDFSADFSDF")
v=test.lower()
print(v)




asdfasdfasdfsadfsdf

  

7:replace

replace(self, *args, **kwargs): # real signature unknown
    """
    Return a copy with all occurrences of substring old replaced by new.
    
      count
        Maximum number of occurrences to replace.
        -1 (the default value) means replace all occurrences.
    
    If the optional argument count is given, only the first count occurrences are
    replaced.
    """
替换所选字符   replace(“文本内的字符”,“想要替换文本的字符”,“替换的数量”)
test=("ASDFASDFASDFSADFSDF")
v=test.replace("ASD","aaa",1)
print(v)



aaaFASDFASDFSADFSDF


test=("ASDFASDFASDFSADFSDF")
v=test.replace("ASD","aaa")
print(v)



aaaFaaaFaaaFSADFSDF

  

原文地址:https://www.cnblogs.com/panyizuoshan/p/10584661.html