[Python] Chapter 02-05 Basics of Python programs (string operations / methods)

[Python] Chapter 02-05 Manipulating strings

"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.

Extracting a character string (extracting a single character)

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'

String retrieval (multiple string retrieval), slice

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'

Methods for strings

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.

Make the string lowercase or uppercase only at the beginning.

<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'

Method for counting strings

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

Split or insert

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'%'.

Various methods

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.

Methods related to strings

In addition to that, there are various methods such as methods related to handling numerical values, so please check them.

Various methods

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.

Finally

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.

Return to [Table of Contents Link]

Recommended Posts

[Python] Chapter 02-05 Basics of Python programs (string operations / methods)
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
[Python] Chapter 02-03 Basics of Python programs (input / output)
[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
Basics of Python ①
Basics of python ①
[Python] Chapter 02-04 Basics of Python Program (About Comments)
[Python3] Understand the basics of file operations
Basics of Python scraping basics
[Python of Hikari-] Chapter 05-06 Control Syntax (Basics of Comprehension)
Summary of string operations
# 4 [python] Basics of functions
Basics of network programs?
[Python for Hikari] Chapter 09-01 Classes (Basics of Objects)
Basics of python: Output
Basics of Python learning ~ What is a string literal? ~
[Basics of Modern Mathematical Statistics with python] Chapter 1: Probability
I tried to summarize the string operations of Python
Summary of python file operations
Summary of Python3 list operations
String object methods in Python
Various Python built-in string operations
Static analysis of Python programs
python: Basics of using scikit-learn ①
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Getting Started with Python Basics of Python
About the basics list of Python basics
Learn the basics of Python ① Beginners
[Basics of Modern Mathematical Statistics with python] Chapter 3: Typical Probability Distribution
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
Python basics ⑤
[Learning memo] Basics of class by python
[Python3] Understand the basics of Beautiful Soup
(Java, JavaScript, Python) Comparison of string processing
Python basics ④
python string comparison / use'list'and'in' instead of'==' and'or'
I didn't know the basics of Python
Python basics ③
Python basics
Basic grammar of Python3 system (character string)
[Basics of python basics] Why do __name__ == "__main__"
Python string
[Python] Chapter 05-02 Control Syntax (Combination of Conditions)
Python learning memo for machine learning by Chainer Chapter 13 Basics of neural networks
Python basics
Python basics
Python basics ③
Python basics ②
[Introduction to Data Scientists] Basics of Python ♬
A memorandum of python string deletion process
[Basics of modern mathematical statistics with python] Chapter 2: Probability distribution and expected value
[Python / DynamoDB / boto3] List of operations I tried
The contents of the Python tutorial (Chapter 5) are itemized.
The contents of the Python tutorial (Chapter 4) are itemized.
The contents of the Python tutorial (Chapter 2) are itemized.
The contents of the Python tutorial (Chapter 8) are itemized.
The contents of the Python tutorial (Chapter 1) are itemized.