[PYTHON] I tried to find out what I can do because slicing is convenient

This is Rem, a liberal arts programmer. Recently, as a personal study, I've been knocking 100 language processes, but I'm impressed with how convenient the slices are.

I don't operate such character strings in Java that I usually use (maybe I just don't know because of lack of study, please tell me who knows).

What is a slice in the first place?

Slice notation selects a range of elements in a sequence object (string, tuple or list). Slice notation can be used as an expression, as an assignment or as a del statement. Quote: http://docs.python.jp/2.7/reference/expressions.html?highlight=%E3%82%B9%E3%83%A9%E3%82%A4%E3%82%B9

I see. Everyone wants that the primary evaluation value result must be a sequence type object.

I wonder if it's a little easier to understand. .. ..

Try using it anyway

The slice can specify any part of the character string by specifying the start position, end position, and step width.

sequence [Start position: End position: Step width]

Also, it seems that these can be omitted. Let's try it with a simple example!

Sample.py


str = u"It's sunny today!"
print (str[::2])

Result: Heaven now!

This time, the step width is specified by 2, so it looks like this. When the step width is 2, the lie is taken out every two elements such as the 0th element, the 2nd element, and so on. By the way, since the start position is 0, beginners cannot retrieve the desired element unless they are careful.

This time, the start position and end position are specified.

Sample.py


str = u"It's sunny today!"
print (str[1:3:])

Result: sunny day

In this way, it specifies the character string from the start to the end.

By the way, if you specify the end position, start position, and step width as something that does not exist in the character string, nothing will be extracted.

Slicing is profound. However, it may reduce readability, so it may not be good to specify it with a negative value.

Recommended Posts

I tried to find out what I can do because slicing is convenient
I tried to find out if ReDoS is possible with Python
I tried to find out what would happen if I converted NaN or INF to int
I tried to find out the outline about Big Gorilla
python beginners tried to find out
I tried to find out in which language that software that I always take care of is written
I tried to find out if m is included in what is called a range type or range such as n..m and range (n, m).
I tried to find 100 million digits of pi
I tried Python! ] I graduated today from "What is Python! Python!"!
I tried to explain what a Python generator is for as easily as possible.
I tried to find out how to streamline the work flow with Excel x Python ②
I tried to scrape YouTube, but I can use the API, so don't do it.
I tried to find out how to streamline the work flow with Excel x Python ⑤
I tried to find out how to streamline the work flow with Excel x Python ①
AtCoder Beginner Contest 177 Problem C I tried to find out why it was wrong
I realized that it is nonsense to use the module without thinking because it is convenient.
I tried to find out how to streamline the work flow with Excel x Python ③
curl: (60) What to do when Issuer certificate is invalid.
pipenv shell is no longer available ... what to do?
I tried to find an alternating series with tensorflow
What to do if pyenv is not enabled (zsh)
I tried to debug.
I tried to paste
I tried to find the entropy of the image with python
What to do when Japanese is not displayed on matplotlib
What to do when PyCharm font is strange or garbled
I tried to summarize SQLAlchemy briefly (There is also TIPS)
What to do if Linux VLC can no longer rotate
Why django-import-export import is so slow and what to do
I stumbled on TensorFlow (What is Out of GPU Memory)
I tried to find out how to streamline the work flow with Excel × Python, my article summary ★
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
I tried to learn PredNet
I tried to organize SVM.
I tried to implement PCANet
I tried to reintroduce Linux
I tried to introduce Pylint
I tried to summarize SparseMatrix
I tried to touch jupyter
I tried to implement StarGAN (1)
I tried Python! ] Can I post to Kaggle on my iPad Pro?
I tried to summarize what was output with Qiita with Word cloud
What to do if "Unnamed: 0" is added in to_csv-> read_csv in pandas
What to do if the inode is exhausted on EC2 Linux
I tried to cut out a still image from the video
What to do when you get "I can't see the site !!!!"
[Machine learning] I tried to do something like passing an image
What to do when the value type is ambiguous in Python?