In the process of learning to use Python, we often use strings to deal with many problems, especially when dealing with format conversion of some algorithmic problems. There are many methods of operating strings, which will be introduced in detail in the next few sections. Let’s look at how to use strings.
In the previous string learning, we briefly mentioned the escape characters of strings. We often intersperse some escape characters in the code to implement line breaks and carriage returns in some codes. We often use them during data analysis. Original string method, here we introduce the original string.
Let's look at the following lines of code:
>>>print('www.dotcpp.comn','Learning Paradise')www.dotcpp.com Learning Paradise>>>print(r'www.dotcpp.comn','Learning Paradise')www. dotcpp.comnLearning Paradise
The 'n' in the first line of code is implemented during output, so the newline operation is implemented.
The string output by the second line of code has 'r' in front of it, so only the original string will be output when outputting. No matter what escape characters are contained in the string, they will not be implemented.
In the process of learning, we will encounter the problem of accessing each character in the string, so we need to use a loop to access each character in the string. We usually use a for loop and index to access.
The format of using a for loop to access a string is:
foriinmy_str
Let's look at an example:
>>>my_str='www.dotcpp.com'>>>foriinmy_str:...print(i,end='')...www.dotcpp.com
We access each element in the string in a loop.
Strings also use indexing, so when we access the characters of strings, we can also use indexing to access them.
Look at the code below:
>>>my_str='www.dotcpp.com'>>>foriinrange(len(my_str)):...print(my_str[i],end='')...www.dotcpp.com
We introduced the structure of range(len()) before. Let’s explain it again. Range() is an object, and len() finds the length. len(my_str) finds the length of the string, and then range( The object of len(my_str)) is 0 to the maximum length of the string minus 1.
i is the value we obtain each time, from 0 to the maximum value of the string length minus 1, and then output my_str[i] through the index value, thus obtaining each element.
We can ask for several elements at will:
>>>my_str'www.dotcpp.com'>>>my_str[5]#The character with index value 5'o'>>>my_str[0]#The character with index value 0, which is the first character' w'>>>my_str[len(my_str)-1]#The index value is the last character 'm'>>>my_str[-1]#This method also accesses the last character 'm'
Proper use of the len() function when using strings can optimize our code.
Strings have their own connection methods. When connecting, we can use ' + ' to directly connect or append one string to the end of another string.
Look at the following example:
>>>my_str'www.dotcpp.com'>>>his_str='Life is short, I use Python'>>>my_str+his_str'www.dotcpp.com Life is short, I use Python'
Strings can be concatenated directly through '+'. After concatenation, a new string is generated, and the original string remains unchanged.
We have used slicing many times in the use of the previous list. Strings can also use slicing expressions to obtain part of the characters in the string. At the same time, we can directly reverse the string through slicing.
Look at the following example:
>>>my_str'www.dotcpp.com'>>>my_str[0:2]#Access 0-1'ww' through slicing>>>my_str[3:6]#3-5'.do'>>> my_str[7:10]#7-9'cpp'>>>my_str[::2]#Access the entire string 'wwdtp.o'>>>my_str[::-1]#Inversion with a step size of 2 String 'moc.ppctod.www'
The next section will learn several commonly used methods in strings.