string common method python3

Find () method # 
Find () to find the range of # substring index value is returned, not found returns -1 

# Syntax 
s.find (the substring, Start = 0, End = len (String)) 
# Parameter 
# the substring - Specifies string search 
# start - start index, the default is 0. 
End # - the end of the index, the default is the length of the string. 

Example # 
S = ' Python ' 
s.find ( ' TH ' )
 # 2 
s.find ( ' TH ' , 1,2 )
 # -1 
s.find ( ' TH ' , l, 3 )
 # -1 
s.find ( ' TH' , 1, 4 )
 # 2 
s.find ( ' TB ' , 1, 4 )
 # -1 
method count () 
number in a character string # statistics appear. 

# Syntax 
s.count (substring, Start = 0, End = len (String)) 
# Parameter 
# Sub - substring search 
# Start - the string to start the search. The default is the first character, the first character index value of zero. 
End # - string ending search for the position. Character index of the first character is 0. The default is the last position of the string. 

Example # 
S = ' password ' 
s.count ( ' S ' )
 # 2 
s.count ( ' S ' , 0,2) 
# 0 
s.count ( ' S ' , 0,3 )
 #. 1 
join () # Method 
join () # Method for the sequence element to specify a character string to generate a new connection. 

# Syntax 
s.join (Sequence) 
# Parameter 
# Sequence - to generate a sequence of elements of the string. 

Example # 
LS = [ ' P ' , ' Y ' , ' T ' , ' H ' , ' O ' , ' n- ' ] 
S = ''  # splice characters 
s.
Python ' 
S   = ' - ' 
s.join (LS) 
#' Python ' 
# Replace () method 
# Replace () method of the string old (old string) replaced with new (new String) 

# Syntax 
s. replace (Old, new new [, max]) 

# parameter 
Old - to be replaced substring. 
new new - new string for replacing old substring. 
max - optional string, alternatively no more than max times 
# exemplary 

S = ' Hello Word ' 
s.replace does ( ' Word ' , ' Python ' )
 ' Hello Python ' 
S= ' Helloword ' 
s.replace does ( ' - ' , '' )
 ' Helloword ' 
s.replace does ( ' - ' , '  ' )
 ' Hello Word ' 
strip () method 
strip () method for removing a string head end of the specified character (space by default) or a sequence of characters. 

# Syntax 
s.strip ([chars]) 
# Parameters 
chars - removing head and tail string of the specified sequence of characters. 

Example # 
S = '   ABCDEFG    ' 
s.strip () 
' ABCDEFG ' 
S = '' 
S.strip ( ' 123 ' )
 '   123abcdefg123    ' 
S = ' 123abcdefg123 ' 
s.strip ( ' 123 ' )
 ' ABCDEFG ' 
# Description 
# character spaces before and if required removing the new space to remove the specified character 

split () the method 
split () designated by the slice delimiter string 

# syntax 
s.split (STR = "" , [NUM]) 
# parameters 
STR - separator, default for all null characters, including spaces, linefeed (\ n ), tab (\ t) and the like. 
NUM - the number of divisions. 

Example # 
S = ' the this IS String ' 
S.
[ ' The this ' , ' IS ' , ' String ' ] 
s.split ( ' S ' , 1) # cutting a 
[ ' Thi ' , ' IS String ' ] 
The method of removing a string spaces #
 # 1 , string functions Replace ()

 >>> S = '   Hello World    '
 
>>> s.replace does ( '  ' ,   '' )
 ' HelloWorld ' 
# 2 , using the string functions Split ()

 >>>s = ' hello world ' 
>>> S = '  ' .join (s.split ())
 >>> Print (S) 
HelloWorld 
# eval () method 
# eval () function is used to perform a string expression, and returns the value of the expression . 

Example # 
DEF eval_test (): 
    L = ' [1,2,3,4, [5,6,7,8,9]] ' 
    D = " { 'A': 123, 'B': 456, 'C ': 789} " 
    T = ' ( ' A ' , ' B ' , ' C ' , ' D ' ) '. 3 * 2 ' 
    Print  ' -------------------------- conversion starts ---------------- ---------------- ' 
    Print type (L), type (the eval (L))
     Print type (D), type (the eval (D))
     Print type (T), type (the eval (T))
     Print (the eval (A))
 IF  the __name__ == " __main__ " : 
    eval_test () 
# output


 ----------------------- --- transition beginning -------------------------------- 
<type ' STR ' > <type ' List ' > 
< type ' STR ' > <type 'dict' > 
<Type ' STR ' > <type ' tuple ' > 
. 6 
# Other methods 
s.capitalize () # initials 

s.lower () # small letter 

s.upper () # revolutions uppercase 

s.swapcase () # Size write interchangeable 

len (STR) # string length 

CMP ( "My Friend", STR) # string comparison of a large return in table 1 according to ascii code comparison 

max ( ' abcxyz ' ) # find the largest character string 

min ( ' abcxyz ' ) # find the smallest character string

s.startswith () ( ' ab ' ) # determines whether the string ab beginning 

s.endwith ( ' xz ' ) # determines whether the string end xz

 

Guess you like

Origin www.cnblogs.com/kongtongshu/p/10993149.html