String manipulation in python

Abstract: This paper introduces some basic operations in python string.

1, the slicing operation

Slicing operation is called with a selected portion of an element composed of regular operation of the new string in the given string .

Using   str [Parameter 1: Parameter 2: Parameter 3]   in the form of operations:

  • Parameter 1: start position of the subscript, defaults to the first position
  • Parameter 2: end position, need to specify, no default parameter contains two elements subscripted
  • 3 parameters: the step size, default is 1, will be applied to the first parameter corresponding operation completion
  • Three parameters can be negative, negative first two parameters is an indication position calculation starts from the last element: -1, -2, -3 ......
  • Parameter 2 is the logarithm direction, this direction should match the parameters and 3, it would not output a corresponding sequence
. 1 password = ' 0123456789 ' 
2  
. 3  Print (password [0:. 5:. 1])   # 01234 ------ basic operation 
. 4  Print (password [. 1:. 5: 2])   # 13 is ------- - step operation 
. 5  
. 6  Print (password [:. 5:])     # 01234 ------ default operation 
. 7  
. 8  Print (password [-1: -11: -1])   # 9876543210 ------ - reverse operation 
. 9  Print (password [:: -. 1])        # 9876543210 -------- default reverse operation 
10  
. 11  Print (password [-2: -9: -2])    # 8642 ---- ----- steps in reverse order 
12  Print(password [-9: -2:. 1])     # 1234567 ------ n sequential operation 
13 is  
14  Print (password [0:. 8: -2])     # no output 
15  Print (password [-2: -9 : 1])    # no output

2, the search operation

Find operation commonly used in python there: find, index, count, rfind, rindex

find ( 'target sub-string', position 1, position 2) 

  • Return Value: Returns the position if found, or -1 if not found
  • The next two parameters is omitted, to find the target in the entire string substring

index:

  • And find the same, with one difference: not find the substring, then an error

count:

  • Return Value: the number of the sought substring

rfind:

  • And find the same, just start looking from the right

rindex:

  • And the same index, just start looking from the right
 1 str='hello python nice to meet you'
 2 
 3 print(str.find('nice'))  #13
 4 print(str.find('nice',10,17))  #13
 5 print(str.find('hi'))  #-1
 6 
 7 print(str.index('nice'))  #13
 8 print(str.index('nice',10,17))  #13
 9 # print(str.index('hi'))  # erroy
10 
11 print(str.count('nice'))  #1
12 print(str.count('nice',10,17))  #1
13 print(str.count('hi'))   #0
14 
15 print(str.rfind('nice'))  #13
16 print(str.rfind('nice',10,17))  #13
17 print(str.rfind('hi'))  #-1
18 
19 print(str.rindex('nice'))  #13
20 print(str.rindex('nice',10,17))  #13
21 #print(str.rindex('hi'))  # erroy

 3, modify operation

Modifying operation in python has commonly used: replace, split, join three important ways. Another case conversion, delete blank cells, such as its non-critical method

Replace ( 'substring old', 'new substring', replacing the number)

  • Returns: a new string
  • New for a long time

Split ( 'split flag substring', to find the number of times)

  • Returns: List
  • After generating a list of spaced apart, and the marker delete substring

Split string. Join (list)

  • Returns: String
  • The list elements in order to connect with a string divided
 1 str='hello python nice to meet you'
 2 
 3 newstr=str.replace('to','kkk',1)
 4 print(str)    # hello python nice to meet you
 5 print(newstr)  # hello python nice kkk meet you
 6 
 7 list=str.split('to')
 8 print(list)   #['hello python nice ', ' meet you']
 9 
10 List=['aa', ' BB ' , ' CC ' , ' dd ' ]
 . 11 newstr = ' and ' .join (List)
 12 is  Print (newstr)      # AA and BB and CC and dd 
13 is  
14 myStr = "   Hello World and the Python and itcast and itheima   " 
15  
16  # . 1, capitalize () string initials 
. 17 new_str = mystr.capitalize ()
 18 is  Print (new_str)
 . 19  # 2.title (): string of each word capitalized 
20 is new_str =mystr.title ()
 21 is  Print (new_str)
 22 is  # 3. Upper (): turn lowercase uppercase 
23 is new_str = mystr.upper ()
 24  Print (new_str)
 25  # 4. Lower (): turn uppercase to lowercase 
26 is new_str = myStr. Lower ()
 27  Print (new_str)
 28  
29  # 1. the lstrip (): delete the left margin character 
30 new_str = mystr.lstrip ()
 31 is  Print (new_str)
 32  # 2. The rstrip (): delete the right margin character 
33 is new_str = mystr.rstrip ()
 34 is  Print (new_str)
 35 # 3.strip():删除两侧空白字符
36 new_str = mystr.strip()
37 print(new_str)
38 
39 '''  输出
40 hello world and itcast and itheima and python  
41   Hello World And Itcast And Itheima And Python  
42   HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON  
43   hello world and itcast and itheima and python  
44 hello world and itcast and itheima and Python  
45   hello world and itcast and itheima and Python
46 hello world and itcast and itheima and Python
47 
48 '''

 Its operation:

 1 >>> mystr='hello'
 2 >>> mystr.ljust(10)
 3 'hello     '
 4 >>> mystr.rjust(10)
 5 '     hello'
 6 >>> mystr.ljust(10,'.')
 7 'hello.....'
 8 >>> mystr.rjust(10,'.')
 9 '.....hello'
10 >>> mystr.center(10)
11 '  hello   '

 4, the operation is determined

The determination operation in python are commonly used letter determination, determining numbers, alphanumeric combinations judgment, judgment spaces.

. 1 myStr = " Hello World and itcast and itheima and the Python " 
2  
. 3  # 1. startsWith (): determines whether the string beginning with a substring 
. 4  Print (mystr.startswith ( ' Hello ' ))   # T 
. 5  Print (myStr. startsWith ( ' HEL ' ))     # T 
. 6  Print (mystr.startswith ( ' HELS ' ))    # F. 
. 7  
. 8  
. 9  # 2. endsWith (): determines whether the string that ends with a substring 
10  Print (mystr.endswith ( 'Python ' ))    # T 
. 11  Print (mystr.endswith ( ' Pythons The ' ))   # F. 
12 is  
13 is  
14  # 3. the isalpha (): letters 
15  Print (mystr.isalpha ())   # F. 
16  
. 17  # 4. isdigit () : digital 
18 is  Print (mystr.isdigit ())   # F. 
. 19 MyStr1 = ' 12345 ' 
20 is  Print (mystr1.isdigit ())   # T 
21 is  
22 is  # 5. The isalnum (): numbers or letters or a combination of 
23 print(mystr1.isalnum())  #T
24 print(mystr.isalnum())   #F
25 mystr2 = 'abc123'
26 print(mystr2.isalnum())  #T
27 
28 
29 # 6.isspace(): 空白
30 print(mystr.isspace())  #F
31 mystr3 = '   '
32 print(mystr3.isspace())  #T

Guess you like

Origin www.cnblogs.com/lzy820260594/p/11789361.html