Python string operations using slices

First, use [] to extract characters:

  The nature of the string is a sequence of characters, we can later add a string [] by adding the offset can be a single character in the position where []

Second, the forward search:

                 Measuring a first left-most character, the offset is 0, the offset is 1 second, until len (str) -1

       Search direction:

                  Rightmost character first, offset -1, -2 penultimate offsets, and so on, until -len (str)

                  如:  f  = "12345678"   f[0] = 1  f[-1] = 8

 Three, replace () implemented string replacement

                   如:  f = "12345667"
       f1 = f.replace("1","a")
                          print(f1)

  Fourth, the slice string slicing operation

          Slice slice operation allows us to quickly extract the string, a standard format: [start: end: step step]

         

 

        

 

       Example:

            f = "abc"   f[::-1]  ==》 "cba"    f1 = "abc"  f[::2] ==> "ac"

                                  

Guess you like

Origin www.cnblogs.com/yingxiongguixing/p/12168526.html