Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

3. General use of python strings


May 10, 2021 Python2


Table of contents


3. General use of Python strings

1. Basic operation

1) Use the connection with

  >>> "abc"+"efg"  
  'abcefg'  
  >>> a="abc"  
  >>> b="efg"  
  >>> c=a+b  
  >>> c  
  'abcefg'  
  >>>   

2) Repeat with

 >>> "abc"*3  
  'abcabcabc'  
  >>> "abc\n"*3  
  'abc\nabc\nabc\n'  
  >>> print("abc\n"*3)  
  abc  
  abc  
  abc  
    
  >>>   

3) Strings cannot be connected with numbers

  >>> "abc"+9  
  Traceback (most recent call last):  
    File "", line 1in   
      "abc"+9  
  TypeError: Can't convert 'int' object to str implicitly  

4) Use for to iterate

 >>> a="abcdefg"  
  >>> for x in a:print(x)  
    
  a  
  b  
  c  
  d  
  e  
  f  
  g  

5) Use in to find characters, and then we'll talk about the str.find() method, which is very similar to in

  >>> a="abcdefg"  
  >>> "h" in a  
  False  
  >>> "abc" in a  
  True  
  >>>   

2. Indexes and shrapned

1) Index

Strings can be indexed to find the characters they want, and the index is divided into positive and negative, looking in different query directions

  >>> a="abcdefg"  
  >>> a[0],a[1],a[5]  
  ('a''b''f')  
  >>> a[-2],a[-1],a[-5]  
  ('f''g''c')  
  >>>   

2) Fragmentation, to be clear, is to extract parts of the string, and when the index uses a negative number, it returns empty

  >>> a="abcdefg"  
  >>> a[1:]  
  'bcdefg'  
  >>> a[1:3]  
  'bc'  
  >>> a[-1:3]  
  ''  
  >>>   

I'll use a diagram to illustrate how the piece is divided, with the example of a .1:3

3. General use of python strings

From above the figure we know better, for 1, he is putting the pointer behind a, starting with b, a does not count, and then after 3, he is putting the pointer in front of d, not d

So only bc is returned

There are also some examples that need to be specific:

a1: From the beginning to the end of the second character

Starts with the first character to the second character, which is before the third character

Copy all characters

In fact, there is a third parameter, step, to be clear, is to jump selection, is to see a few jumps

  >>> a="abcdefghijklmn"  
 >>> a[2:8:2]  
 'ceg'  
  >>> a[::3]  
  'adgjm'  
  >>>   

a (2:8:2) means to start with the third character, to the seventh before the eighth, and then pick one every 2 out

a .::3) stands for picking one out of every three characters

If the step is plual, pick the other way around

3. String conversion str() and repr()

  >>> str(42)  
  '42'  
  >>> repr(43)  
  '43'  

The difference is that when using print, repr has a pair of single quotes

  >>> print(str('aaa'),repr('aaa'))  
  aaa 'aaa'  

4. Modify the string

Strings can't be modified, so how do you modify them?

The process is that we extract some characters from one string and then add others to form a new string object

 >>> a="abcdefghijklmn"  
  >>> id(a)  
  24338048  
  >>> b=a[1:5]+" \n this is a str"  
 >>> id(b)  
  24342896  
 >>> b  
  'bcde \n this is a str'  
  >>> print(b)  
  bcde   
  this is a str  
 >>>