Cut a part of the string using a Python slice

Introduction

A convenient slicing function when cutting a character string in Python.

I used to specify the characters to cut out, but many of the explanations didn't come to my mind.

 Here, I will post my own interpretation.

# Slice ``` []` `
```python
str_1 = 'python'
print(str_1[2:6])
# 'thon'

For example, if you want to extract only the thon of the string'python', you can use slices and specify [2: 6], which is a very convenient function. However, I feel uncomfortable with the method of setting the numbers here.

This is because the slice is specified by [Start position: End position], but the start position is the number counting the character string from 0, and the end position is the number counting the character string from 1. I don't think it's going to happen once you get used to it, but it doesn't feel right at all.

Try to change the way of thinking (turn around)

The following is what I rewrote according to my own interpretation.

str_1 = 'python'
start = 2
count = 4
print(str_1[start:start+count])
# 'thon'

First, specify from which character to count with start. Since t is the second character counting from 0, specify 2.

Next, specify how many characters you want to retrieve from the start position with count. Since 'thon' is 4 characters, specify 4.

Slices are interpreted this way. [(Start position): (Start position + number of characters)] Specify it with [start: start + count] on the program. This is synonymous with the case specified by [2: 6], but I think it is a nice explanation.

Finally

When you actually write a program, I don't think you need to write it in such a roundabout way. However, you can reduce mistakes by remembering when you are unsure of the specified method. I don't know how the slice was developed, so I don't know, but it would be nice if it was developed with the idea described here. .. ..

Recommended Posts

Cut a part of the string using a Python slice
Explanation of the concept of regression analysis using python Part 2
Explanation of the concept of regression analysis using Python Part 1
Find out the apparent width of a string in python
python string slice
[python] [meta] Is the type of python a type?
Finding the optimum value of a function using a genetic algorithm (Part 1)
The story of blackjack A processing (python)
A memorandum of python string deletion process
I want to color a part of an Excel string in Python
[Python] Read the source code of Bottle Part 2
Get the caller of a function in Python
converting argument $ 1 type: unsupported type [] string, a slice of string
Make a copy of the list in Python
Try using the Python web framework Tornado Part 1
A note about the python version of python virtualenv
[Python] Read the source code of Bottle Part 1
[Python] A rough understanding of the logging module
Output in the form of a python array
Try using the collections module (ChainMap) of python3
Find the geometric mean of n! Using Python
Try using the Python web framework Tornado Part 2
A discussion of the strengths and weaknesses of Python
[Python + OpenCV] Whiten the transparent part of the image
the zen of Python
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
Python> Slice> A slice offset earlier than the beginning of a string is treated as 0, and one after the end is treated as -1
I made a script to record the active window using win32gui of Python
[Python] Implementation of clustering using a mixed Gaussian model
Check if the string is a number in python
[Python] A program that counts the number of valleys
Get the variable name of the variable as a character string.
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
A python implementation of the Bayesian linear regression class
Python points from the perspective of a C programmer
# Function that returns the character code of a string
The pain of gRPC using Python. November 2019. (Personal memo)
Basics of Python learning ~ What is a string literal? ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
[Python] Mask the image into a circle using Pillow
Tasks at the start of a new python project
Explanation of the concept of regression analysis using Python Extra 1
Study from the beginning of Python Hour8: Using packages
A reminder about the implementation of recommendations in Python
I tried using Python (3) instead of a scientific calculator
[Python] A program that compares the positions of kangaroos.
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
A little bit from Python using the Jenkins API
Python Note: The mystery of assigning a variable to a variable
I tried to summarize the string operations of Python
[Ruby] How to replace only a part of the string matched by the regular expression?
Python: Calculate the uniform flow depth of a rectangular cross section using the Brent method
Towards the retirement of Python2
[Python] Use a string sequence
About the ease of Python
python: Basics of using scikit-learn ①
[Python 2/3] Parse the format string
About the features of Python