Study from Python Hour4: Object-oriented ②

Study from Python Hour4: Object-oriented ②

Learning materials

Past posts

environment

Immutable object

Manipulating immutable objects

>>> my_str = 'hello Python!!'
>>> my_str2 = my_str.upper()
>>>
>>> ###Show original value
>>> print('Original: ', my_str)
Original:  hello Python!!             ###You can see that it has not changed
>>>
>>>
>>> ###Display the value after string operation
>>> print('Return my_str2: ', my_str2)
Return my_str2:  HELLO PYTHON!!


>>> my_str = 'hello Python!!'
>>> my_str2 = my_str.replace('hello', 'kill')
>>>
>>>  ###Show original value
>>> print('Original: ', my_str)  
Original:  hello Python!!                 ###You can see that it has not changed
>>>
>>>
>>> ###Display the value after string operation
>>> print('Return my_str2: ', my_str2)
Return my_str2:  kill Python!!

Let's play with the method of the string type instance

>>> ###format method. The one who is taken care of by string concatenation
>>> in_name = 'Pochi'              ###Specify name and age with variables
>>> in_age  = 3
>>>
>>>### {}Insert the value of the variable inside. This is a fashionable method
>>> my_text = 'My name is {}. {} years old.'.format(in_name, in_age)
>>> print(my_text)
My name is Pochi. 3 years old.
>>>

>>> ###startswith method. Check if there is a beginning of the character string. bool type and True/False will be returned.
>>> print('AHV-log:aaa'.startswith('AHV'))     ###It seems easy to use if there is a log file name at the beginning of the log
True
>>> print('mail-log:aaa'.startswith('AHV'))
False

>>> ###Same usage for endswith method


>>> ###strip method. Remove whitespace before and after the string
>>> print('  aaa    ') 
  aaa
>>> print('  aaa    '.strip())
aaa     ###The leading and trailing spaces have been removed. Useful when dealing with stupid log files
>>>

>>> ###split method. When retrieving data separated by commas. Can be used with JSON
>>> my_text ='aaa,bbb,ccc'
>>> my_list = my_text.split(',')    ###Split with commas
>>>
>>> print(my_list)
['aaa', 'bbb', 'ccc']
>>>

>>> ###Join with join method (note that you are doing it with a text object method, not a list)
>>> my_list = ['aaa', 'bbb', 'ccc']
>>> print(','.join(my_list))
aaa,bbb,ccc         ###I was able to separate the list with commas
>>>

>>> ###String comparison in operator (whether string A is contained in string B). It seems to be used in a conditional expression
>>> print('aaa' in 'aaaaaaa')
True
>>> print('aaa' in 'baaaaaa')
True
>>> print('aaa' in 'aaabbbaaa')
True
>>> print('aaa' in 'ccc')
False
>>>

Method chain

>>> my_name = 'pochi'
>>> my_intro = 'My name is {}.'.format(mnname)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'mnname' is not defined
>>> my_intro = 'My name is {}.'.format(my_name)
>>> my_intro.upper()
'MY NAME IS POCHI.'      ###This much processing
>>>


>>> my_name = 'pochi'
>>> print('My name is {}.'.format(my_name).upper())
'MY NAME IS POCHI.'    ###Can be done in one line
>>>

This summary

For Quotations / Lightning Talk

Recommended Posts

Study from Python Hour4: Object-oriented ②
Study from Python Hour4: Object-oriented ①
Study from Python Hour3: Functions
Study from Python Hour2: Control statements
Study from Python Hour7: How to use classes
Study from Python Reading and writing Hour9 files
Study from the beginning of Python Hour1: Hello World
Study from the beginning of Python Hour8: Using packages
Python study note_002
Python study notes _000
Python study notes_006
Python study note_004
sql from python
MeCab from Python
Python study note_003
Python study notes _005
Python study notes_001
Python study day 1
Study from Python Hour6: Frequently used data types: tuple type, set type, dictionary type
Use thingsspeak from python
Touch MySQL from Python 3
"Object-oriented" learning with python
Operate Filemaker from Python
Use fluentd from python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Use MySQL from Python
Run python from excel
Install python from source
Execute command from Python
Operate neutron from Python!
Use MySQL from Python
Operate LXC from Python
Manipulate riak from python
Force Python from Fortran
Use BigQuery from python.
Execute command from python
[Python] Read From Stdin
Use mecab-ipadic-neologd from python
Flatten using Python yield from
Call CPLEX from Python (DO cplex)
Deep Python learned from DEAP
Post from Python to Slack
Grammar features added from Python3.6
Cheating from PHP to Python
Make MeCab available from Python3
Information obtained from tweet_id (Python)
OCR from PDF in Python
Study Python with Google Colaboratory
Run illustrator script from python
Use MySQL from Anaconda (python)
Query Athena from Lambda Python
Access Oracle DB from Python
Start / stop GCE from python
Stop Omxplayer from Python code
python pandas study recent summary
Switch from python2.7 to python3.6 (centos7)
[WIP] Fluent Python Study Note
Connect to sqlite from python
Install pyenv from Homebrew, install Python from pyenv