python intercepts contents of the string

 

a = '123_abc'

Consider a string above, if you want to specify which portion is taken out, there are several ways:

 

1. split()

a.split ( ' _ ' ) # results [ '123', 'abc'] 
a.split ( ' _ ' ) [0] # results' 123 ' 
a.split ( ' _ ' ) [. 1] # results' abc '

 

2. index()

= a.index index ( ' _ ' ) # results. 3 
A [: index] # results '123' 
A [index +. 1:] # result 'abc'

index () compared to the split () advantage: You can specify start and end index index, such as a.index ( '_', 0, 5)

 

3. find()

= a.find index ( ' _ ' ) # results. 3 
A [: index] # results '123' 
A [index +. 1:] # result 'abc'

find () compared index () advantage: If the string is not included in '_', find () returns -1, and the index () being given

Guess you like

Origin www.cnblogs.com/patriciaaa/p/11373296.html