"Chapter 02-02" dealt with the basic part about character strings.
This time, I would like to extract a part from those strings and operate using instructions called methods.
A part of the character string can be extracted and displayed. This time I would like to run the program from the ** Python console **.
First, enter the code below.
>>>str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>>print(str)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
The content is assigning the character string "ABCDEFGHIJKLMNOPQRSTUVWXYZ" to the variable str. It is displayed on the screen with the print function.
As you know, there are 26 types of alphabets. To check if there are really 26 types, you can also use the ** len function ** to find out the length of the string.
>>>len(str)
26
I tried to summarize the character strings stored in the str variable in the table below. Also, ** numbers start from 0 **.
number | 0 | 1 | 2 | 3 | 4 | 5 | … | 22 | 23 | 24 | 25 |
---|---|---|---|---|---|---|---|---|---|---|---|
String | A | B | C | D | E | F | … | W | X | Y | Z |
With the above table in mind, I would like to extract the characters. To extract characters, ** enter the number of the extraction position ** in []. Enter the following code in the ** Python Console **.
>>>str[0]
'A'
>>>str[1]
'B'
>>>str[9]
'J'
As many of you may have noticed, in the case of ** Python, the beginning is 0 **. Please note that some programming languages start from 1 at the beginning.
As you can see from the table above, there are only up to 25 numbers. Therefore, if you specify a number that exceeds the length of the character string, an error will occur.
>>>str[26]
Traceback (most recent call last):
File "<input>", line 1, in <module>
IndexError: string index out of range
You can also specify a negative number in []. If you specify a negative number, it will be extracted from the end of the string. Enter the following code in the ** Python Console **.
>>>str[-1]
'Z'
>>>str[-2]
'Y'
>>>str[-9]
'R'
The character string can also be retrieved by specifying the range. This is sometimes called ** slice **. Specify ":" (colon) to specify the range.
Let's actually check it on the ** Python console **. Enter the code below. The character string to be used is the same as before. str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>>str[5:]
'FGHIJKLMNOPQRSTUVWXYZ'
>>>str[:24]
'ABCDEFGHIJKLMNOPQRSTUVWX'
>>>str[5:24]
'FGHIJKLMNOPQRSTUVWX'
First of all, regarding str [5:], the following table presented earlier,
number | 0 | 1 | 2 | 3 | 4 | 5 | … | 22 | 23 | 24 | 25 |
---|---|---|---|---|---|---|---|---|---|---|---|
String | A | B | C | D | E | F | … | W | X | Y | Z |
As you can see in, the range of the 5th and subsequent character strings (after'F') is extracted. Let's express this a little mathematically. (Think of x as a number)
5≦x
And the range including the 5th is taken out.
Next, about str [: 24]. Compared to the table, the 24th'Y' is not included. Note that this is taken to the 24th ** front **. Mathematically,
x<24
Please note that the 24th is not included.
Finally, str [5:24], which is a combination of the above two. The range with the specified beginning and end is extracted. However, as mentioned above, please note that it is extracted to the front of the end. Mathematically,
5≦x<24
It will be.
As a supplement to the extraction by specifying the range, you can extract characters at any character interval. In the example below, it is extracted at intervals of 2 characters.
>>>str[::2]
'ACEGIKMOQSUWY'
You can recognize that it issues a processing instruction to what a ** method ** is. Since we are dealing with character strings this time, it is a processing instruction such as "make all alphabetic character strings lowercase" and "add characters at the end" for the character strings.
The details of the method will be described later.
How to use the method
Target.Method()
Separate them with "." (Dot). There are many methods. I would like to introduce some basic ones.
<font color = # 00cc00> lower () </ font>: You can lowercase the string.
>>>str.lower()
'abcdefghijklmnopqrstuvwxyz'
<font color = # 00cc00> capitalize () </ font>: Only the beginning can be capitalized.
>>>str.capitalize()
'Abcdefghijklmnopqrstuvwxyz'
By the way, you can apply the method directly as below without using the str variable.
>>>'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.capitalize()
'Abcdefghijklmnopqrstuvwxyz'
This time specify a different string. Please enter as follows.
>>>str1 = 'tomorrow'
<font color = # 00cc00> count (characters you want to search) </ font>: Count the characters you want to search.
>>>str1.count('o')
3
In the above example, the number of characters'o'in str1 is counted.
The count method can be specified in more detail, and the range can be specified as follows. In the example below, the character'o'is counted in the range limited to the 3rd and above and before the 5th. (Of course, it is a count from 0.)
str1.count('o',3,5)
1
Specify a different string this time as well. Please enter as follows.
>>>str2 = 'Tanjiro,Nezuko,Zenitsu,Inosuke'
<font color = # 00cc00> split (split character) </ font>: Creates a list in which the character string is divided by the specified character string.
The details of ** list ** will be covered in Chapter 4 and later, but it is okay to recognize that the above example can be divided into four elements.
Use it as follows.
>>>str2.split(',')
['Tanjiro', 'Nezuko', 'Zenitsu', 'Inosuke']
One character string stored in the str2 variable is divided by the "," (comma) part. The output result is enclosed in [], and this is the structure called a list. I was able to divide it into four strings. Details of the list will be described later.
In order to use this list later, we will assign it to a variable called ** ls **.
ls=str2.split(',')
print(ls)
['Tanjiro', 'Nezuko', 'Zenitsu', 'Inosuke']
<font color = # 00cc00> join (join target) </ font>: Combines the disjointed items in the list into one character string.
Do the opposite of the split method above. In other words, the character string divided into four is made into one.
Please enter the following and check the operation.
>>>'%'.join(L)
'Tanjiro%Nezuko%Zenitsu%Inosuke'
In this way, we have created a four-part list into a single string using'%'.
The example introduced above is just one example. There are an infinite number of methods, and in reality, you will often look up methods on the Web and apply them.
Below is a link to the methods related to strings, which is the scope of this time.
In addition to that, there are various methods such as methods related to handling numerical values, so please check them.
These terms, which are mentioned in the link, may be difficult to read at first, but it seems that you will get used to them as you progress little by little.
This time I touched on string processing and methods. Since there are many methods, it seems that rather than memorizing the methods, it will be investigated and applied from the above link as needed. In fact, it is the same in practice.
Please try various methods.
Recommended Posts