What you want to memorize with the basic "string manipulation" grammar of python

(My memo) What I want to memorize with the basic python grammar

Introduction

Write for yourself so that you don't forget the basics of pthon.


Comment out

In all languages, the first thing to remember is "comment out".

python1.py


#Comment out

There seems to be other description methods, but it is enough to remember only #


Slice (basic)

Thrust (** [] ** after the variable) is a python-specific (?) Usage. It can be used quite well. Also, for python beginners, it looks like an array, which makes it difficult to decipher. Even if you remember the function before this, you will stumble on the slice.

python2.py


num="0123456789"

#Get "1st" from the left * When viewed from the left, the leftmost is "0"
a1=num[1]
# '1'

#Get "1st" from "Right" * When viewed from the right, the far right is ""-No. 1 "<I can understand this far
a2=num[-1]
# '9'

#Obtained "3rd to 5th" <Stumble around here. 5th instead of 5
a3=num[3:5]
# '34'

#Obtained "3rd ~" <":Pay attention to the relationship between "" and "..."
a4=num[3:]
# '3456789'

#Get "~ 3rd"
a5=num[:3]
# '012'

#Obtained by "skipping 2 pieces" * Since it starts from 0, it happened to be an even number
a6=num[::2]
# '02468'

#"Reverse order(In order from the back)Get to
a7=num[::-1]
# '9876543210'

In other words, the rule is ** [start: stop: step] **. (Minus is in reverse order)

Slice (application)

python3.py


#Dare to fill in the slice and display it in the same way as the original (default value?)
a1=num[0::1] #a
# '0123456789'

#Can be used in combination
#Obtained by "reverse order" and "skip two". Set to "reverse order". * The end of the character string was 9 which was an odd number, so it happened to be an odd number.
a2=num[::-1][::2][::-1]
# '13579'

#It can also be used as a list type. Obtained by "skipping two"
numl=[0,1,2,3,4,5,6,7,8,9]
n1=numl[::2]
# [0, 2, 4, 6, 8]

** Reference ** If you want to know more about slices, this is a good choice. Explains the processing inside the sequence. https://qiita.com/tanuk1647/items/276d2be36f5abb8ea52e


String search / replace / compare

A common feeling. it's simple.

python4.py


num="0123456789"

#Get the display position
foo=num.find("1")
# 1

#Search and convert to the specified character
foo=num.replace("1","a")
# '0a23456789'

abc1="abc"
abc2="abc"

#Simple comparison is easy with operators
abc1==abc2
# True

#Partial comparison
"bc" in abc1
# True



"Character string ⇒ list", "list ⇒ character string conversion"

The point is that the delimiter is "blank"

python5.py


abc='a b c d e f'
#Convert from character string to list. * Blank delimiter
abcl = abc.split()
# ['a','b','c','d','e','f']

#list(letter)⇒letter列に変換する ※空白区切り
a1=" ".join(abcl)
# 'a,b,c,d,e,f'

numl=[0,1,2,3,4,5,6,7,8,9]
#list(Numbers)⇒Convert to a character string * Blank delimiter <The point is to make it str type in advance with map
n1=" ".join(map(str,numl))
# '0 1 2 3 4 5 6 7 8 9'

Click here for details on join split https://techacademy.jp/magazine/28688 https://note.nkmk.me/python-split-rsplit-splitlines-re/ https://hydrocul.github.io/wiki/programming_languages_diff/string/join.html


Combine characters and remove extra whitespace

Easy to remember

python6.py


st="hello"
ed=" world"

#Combine two characters
l1=st+ed
# 'hello world'

#Erase extra white space
ed.strip()
# 'world'

Recommended Posts

What you want to memorize with the basic "string manipulation" grammar of python
Make a note of what you want to do in the future with Raspberry Pi
I want to output the beginning of the next month with Python
Basic grammar of Python3 system (character string)
I want to extract an arbitrary URL from the character string of the html source with python
If you want to include awsebcli with CircleCI, specify the python version
I want to batch convert the result of "string" .split () in Python
[Introduction to Python] What is the method of repeating with the continue statement?
Links to do what you want with Sublime Text
I want to inherit to the back with python dataclass
Summary of the basic flow of machine learning with Python
[Introduction to Python] Basic usage of the library matplotlib
I tried to summarize the string operations of Python
[Introduction to Python] Basic usage of the library scipy that you absolutely must know
Solution when you want to use cv_bridge with python3 (virtualenv)
I tried to find the entropy of the image with python
[AWS] What to do when you want to pip with Lambda
I want to specify another version of Python with pyvenv
Move what you installed with pip to the conda environment
What I did to welcome the Python2 EOL with confidence
[Python] I want to use the -h option with argparse
Try to automate the operation of network devices with Python
I want to know the features of Python and pip
Basic summary of data manipulation with Python Pandas-First half: Data creation & manipulation
Get the source of the page to load infinitely with python.
[Python] What do you do with visualization of 4 or more variables?
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
Nice to meet you with python
Basic grammar of Python3 system (dictionary)
I want to debug with Python
Python | What you can do with Python
Basic study of OpenCV with Python
Don't write Python if you want to speed it up with Python
A note on what you did to use Flycheck with Python
I want to know the weather with LINE bot feat.Heroku + Python
Become familiar with (want to be) around the pipeline of spaCy
What to do if you can't install pyaudio with pip #Python
Basic grammar of Python3 system (how to use functions, closures, lambda functions)
When you want to save the result of the callback function somewhere
I want to check the position of my face with OpenCV!
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
I tried to improve the efficiency of daily work with Python
When you want to adjust the axis scale interval with APLpy
PhytoMine-I tried to get the genetic information of plants with Python
Summary of character string format in Python3 Whether to live with the old model or the new model
[Django] What to do if the model you want to create has a large number of fields
When you want to use multiple versions of the same Python library (virtual environment using venv)
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
If you want a singleton in python, think of the module as a singleton
Have Alexa run Python to give you a sense of the future
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
How to crop the lower right part of the image with Python OpenCV
What to do if you couldn't send an email to Yahoo with Python.
18 beautiful Python terms you want to read aloud. R18 with example sentences
Comparing the basic grammar of Python and Go in an easy-to-understand manner
Try to image the elevation data of the Geographical Survey Institute with Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
(Python Selenium) I want to check the settings of the download destination of WebDriver
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to analyze logs with Python
When you want to change the HTTP headers of Flask's test client