You will study the basic grammar of Python 3 by referring to "Introduction to Python 3" by O'Reilly Japan. I hope it will be helpful for those who want to study Python in the same way.
Since the string is immutable (the value cannot be changed later), the string cannot be rewritten. For example, when changing the first character of ABC to G, an error will occur if the following method is tried.
>>> target = 'ABC'
>>> target[0] = 'G'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
If you want to rewrite it, use a function etc. to get the operation result as a new character string.
>>> #When using single quotes
>>> 'sample'
'sample'
>>> #When using double quotes
>>> "sample"
'sample'
>>> #Single quote(Three)
>>> '''sample'''
'sample'
>>> #Double quote(Three)
>>> """sample"""
'sample'
>>> #Empty string
>>> ''
''
>>> #Concatenation of literal strings
>>> 'ABC' + 'DEF'
'ABCDEF'
>>> #In the case of literal strings, you can concatenate them just by arranging them.
>>> 'ABC' 'DEF'
'ABCDEF'
>>> #Concatenation of string variables
>>> target1 = 'ABC'
>>> target2 = 'DEF'
>>> target1 + target2
'ABCDEF'
>>> #In the case of string variables, just arranging them will result in an error
>>> target1 = 'ABC'
>>> target2 = 'DEF'
>>> target1 target2
File "<stdin>", line 1
target1 target2
^
SyntaxError: invalid syntax
>>> 'pyon ' * 5
'pyon pyon pyon pyon pyon '
>>> target = 'ABC'
>>> target[0]
'A'
>>> target[1]
'B'
>>> target[2]
'C'
You can use slices to extract substrings from a string.
# [start:end:step]
# start :Start offset
# end :Tail offset
# step :Step (every character)
>>> target = 'ABCDEFGHIJKLMN'
>>> #Get the entire string
>>> target[:]
'ABCDEFGHIJKLMN'
>>> #From offset 5 to the end
>>> target[5:]
'FGHIJKLMN'
>>> #Offset 2 to 5
>>> target[2:5]
'CDE'
>>> #Last 3 letters
>>> target[-3:]
'LMN'
>>> #Offset 0 to 10 every 2 steps
>>> target[0:10:2]
'ACEGI'
>>> #It can be omitted like this
>>> target[:10:2]
'ACEGI'
>>> str(123)
'123'
>>> str(True)
'True'
>>> target = 'ABC'
>>> target.replace('A','G')
'GBC'
>>> target = 'ABCDE'
>>> len(target)
5
>>> #Specify a comma for the separator
>>> target = 'AB,C,DE,F'
>>> target.split(',')
['AB', 'C', 'DE', 'F']
>>> #If no separator is specified, whitespace characters (newlines, spaces, tabs) are treated as separators.
>>> target = 'A B C DE F'
>>> target.split()
['A', 'B', 'C', 'DE', 'F']
The join () function is the inverse of the split () function
>>> #Specify the delimiter, then specify the list
>>> str_list = ['AB', 'C', 'DE', 'F']
>>> target = ','.join(str_list)
>>> print(target)
AB,C,DE,F
>>> target = 'ABCDEFGHIJKLMN'
>>> target.startswith('ABC')
True
>>> target.startswith('BCD')
False
>>> target = 'ABCDEFGHIJKLMN'
>>> target.endswith('LMN')
True
>>> target.endswith('KLM')
False
>>> #If found
>>> target = 'ABCDEFGHIJKLMN'
>>> target.find('D')
3
>>> target.find('DE')
3
>>> #If not found-1 is returned
>>> target.find('DG')
-1
>>> target.find('T')
-1
>>> target = 'AABBBCAAABCCCCCC'
>>> target.count('A')
5
>>> target.count('B')
4
>>> target.count('C')
7
>>> target.count('AA')
2
>>> target.count('D')
0
>>> target = 'thank you for coming.'
>>> target.capitalize()
'Thank you for coming.'
>>> target = 'thank you for coming.'
>>> target.title()
'Thank You For Coming.'
>>> target = 'thank you for coming.'
>>> target.upper()
'THANK YOU FOR COMING.'
>>> target = 'THANK YOU FOR COMING.'
>>> target.lower()
'thank you for coming.'
>>> target = 'AbcdEFg'
>>> target.swapcase()
'aBCDefG'
>>> #Centered in space 40
>>> target = 'thank you for coming.'
>>> target.center(40)
' thank you for coming. '
>>> #Leftmost arrangement in space 40
>>> target = 'thank you for coming.'
>>> target.ljust(40)
'thank you for coming. '
>>> target = 'thank you for coming.'
>>> target.rjust(40)
' thank you for coming.'
Standard documentation for string functions http://bit.ly/py-docs-strings
Recommended Posts