>>> 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!!
>>> ###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
>>>
>>> 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
>>>
Strings are mutable objects so let's understand and use them
It takes an hour to check this amount. It's almost 7 hours passed w
Recommended Posts