Learn Python strings every day
Learn Python strings every day
All character encoding in Python3 is Unicode, so you don't have to worry about encoding.
Basic operations
In Python, strings can be expressed by single quotes or double quotation marks. The most basic operations are as follows:
In[5]: s = "hello world"In[6]: len(s)Out[6]: 11In[7]: s[0]Out[7]: 'h'In[8]: s + '!'Out[8]: 'hello world!'
If the string is long and spans multiple lines, you can enclose the content in three quotation marks:
In[32]: s = '''a... b... c... '''In[33]: sOut[33]: 'a\nb\nc\n'
Note: The In/Out [n] before the command only indicates the number of commands, which has no practical significance.
Format
The string formatting in Python is very simple. It is represented by {n} where the value needs to be inserted. You can directly call the format () function and input the corresponding parameters. Note that the subscript starts from 0, in addition, the number of input parameters must be greater than or equal to the number of required parameters (multiple input parameters will be ignored ):
In[10]: s = 'my name is {0}, my age is {1}'In[11]: s.format('drfish',20)Out[11]: 'my name is drfish, my age is 20'In[12]: s.format('drfish')Traceback (most recent call last): File "
", line 1, in
s.format('drfish')IndexError: tuple index out of rangeIn[13]: s.format('drfish',20,22)Out[13]: 'my name is drfish, my age is 20'
If there are many parameters, it is easy to use numbers to indicate confusion. You can use aliases to represent them:
In[28]: '{name} is {age}. '.format(name="drfish", age=22)Out[28]: 'drfish is 22. '
Let's take a look at the complexity. The input parameter is not only a simple value replacement, but can be processed subsequently in the string, such as passing in a dictionary, you can take the value of a key for it. Similarly, you can use the method attributes of some complex objects:
In[22]: s = 'my name is {0[name]}, my age is {0[age]}'In[23]: s.format({"name":"drfish","age":20})Out[23]: 'my name is drfish, my age is 20'
In addition, the output format can be specified as follows:
# Retain the four digits after the decimal point In [26]: '{0 :. 4} is a decimal. '. format (1/3) Out [26]: '0. 3333 is a decimal. '# Use' _ 'to fill In the string In [27]:' {0: _ ^ 11} is a 11 length. '. format ("drfish") Out [27]: '_ drfish ___ is a 11 length. '# specify the width In [30]: 'My name is {0: 8 }. '. format ('drfish ') Out [30]: 'My name is drfish.'
Common Methods
Case sensitivity
Strings can be used for case-sensitive judgment and conversion:
In[34]: 'hello'.upper()Out[34]: 'HELLO'In[35]: 'Hello'.lower()Out[35]: 'hello'In[36]: 'Hello'.isupper()Out[36]: False
Split
The split function str. split () can receive two parameters: one is the delimiter and the other is the number of splits:
In[37]: 'a=1,b=2,c=3'.split('=',2)Out[37]: ['a', '1,b', '2,c=3']
Count
Count the number of strings in a string:
In[38]: "ab c ab".count("ab")Out[38]: 2
Intercept
The screenshot operation is the same as the List Operation:
In[39]: "hello world"[3:8]Out[39]: 'lo wo'