You can use slices to extract ** substrings ** (parts of a string) from a string.
-[:] Extracts the entire sequence from the beginning to the end. --[start:] extracts the sequence from the start offset to the end. -[: end] extracts the sequence from the beginning to the ** end-1 ** offset. --[start: end] extracts the sequence from the start offset to the ** end-1 ** offset. -[start: end: step] extracts the sequence from the start offset to the ** end-1 ** offset for each step character.
>>> letters = 'abcdefghijklmnopqrstuvwxyz'
#Specify the entire character
>>> letters[:]
'abcdefghijklmnopqrstuvwxyz'
#Cut from offset 20 to the end.
>>> letters[20:]
'uvwxyz'
>>> letters[10:]
'klmnopqrstuvwxyz'
#Cut off offsets 12-14.
>>> letters[12:15]
'mno'
#Take the last 3 letters.
>>> letters[-3:]
'xyz'
#Cut from offset 18 to 4 characters before the end.
>>> letters[18:-3]
'stuvw'
>>> letters[-6:-2]
'uvwx'
#Cut every 7 characters from the beginning to the end.
>>> letters[::7]
'ahov'
#Cut out every 3 characters from offset 4 to 18.
>>> letters[4:19:3]
'ehknq'
>>> letters[19::4]
'tx'
>>> letters[:21:5]
'afkpu'
>>> letters[:21:5]
'afkpu'
#If a negative number is specified as the step size, it will be sliced in reverse.
>>> letters[-1::-1]
'zyxwvutsrqponmlkjihgfedcba'
>>> letters[::-1]
'zyxwvutsrqponmlkjihgfedcba'
#Tolerant of wrong offsets.
>>> letters[-499:]
'abcdefghijklmnopqrstuvwxyz'
#The slice offset before the beginning of the string is 0, and the slice offset after the end is-Treated as 1.
>>> letters[-51:50]
'abcdefghijklmnopqrstuvwxyz'
#From the last 51 characters to the last 50 characters, it is as follows.
>>> letters[-51:-50]
''
>>> letters[70:71]
''
The len () function counts the number of characters in a string.
>>> len(letters)
26
>>> empty=''
>>> len(empty)
0
The string function split () splits a string based on a ** separator ** to create a ** list ** of substrings. Point split () creates a ** list **
>>> todos = "a,b,c,d"
>>> todos.split(",")
['a', 'b', 'c', 'd']
>>> todos = "get gloves,get mask,get cat vitamins,call ambulance"
#By default, the argument whitespace character""Is used
>>> todos.split()
['get', 'gloves,get', 'mask,get', 'cat', 'vitamins,call', 'ambulance']
It works the opposite of the split () function.
>>> crypto_list =["aa","bb","cc"]
#string.join(list)Describe in the format.
>>> crypto_string=",".join(crypto_list)
>>> print("unchi",crypto_string)
unchi aa,bb,cc
>>> poem="""All that doth flow we cannot liquid name
... Or else would fire and water be the same;
... But that is liquid which is moist and wet
... Fire that property can never get.
... Then 'tis not cold that doth the fire put out
... But 'tis the wet that makes it die,no doubt."""
>>> poem[:13]
'All that doth'
#Check the length
>>> len(poem)
249
#Check if the beginning is All
>>> poem.startswith('All')
True
#The end is That\'s all,folks!Whether it is
>>> poem.endswith('That\'s all,folks!')
False
#Find the offset where the word the first appears.
>>> word = 'the'
>>> poem.find(word)
73
#You can also see the offset of the last the.
>>> poem.rfind(word)
214
#How many times did the three-letter sequence the appear?
>>> poem.count(word)
3
#Whether all the letters in this poem are letters or numbers.
>>> poem.isalnum()
False
#From both ends.Excludes the sequence of
>>> setup = 'a duck goes into a bar...'
>>> setup.strip('.')
'a duck goes into a bar'
#Title case for the first word (only the first letter is capitalized)
>>> setup.capitalize()
'A duck goes into a bar...'
#Title case for all words
>>> setup.title()
'A Duck Goes Into A Bar...'
#Uppercase all letters
>>> setup.upper()
'A DUCK GOES INTO A BAR...'
#Convert all characters to lowercase
>>> setup.lower()
'a duck goes into a bar...'
#Reverse case
>>> setup.swapcase()
'A DUCK GOES INTO A BAR...'
#Place the character string in the center of the space for 30 characters
>>> setup.center(30)
' a duck goes into a bar... '
#Placed on the left end
>>> setup.ljust(30)
'a duck goes into a bar... '
#Placed on the right end
>>> setup.rjust(30)
' a duck goes into a bar...'
You can easily rewrite a substring using replace (). Replace () is good when the part you want to rewrite is clear.
>>> setup = 'a duck goes into a bar...'
#If the last number is omitted, the replacement is only once.
>>> setup.replace('duck','aaaaaaa')
'a aaaaaaa goes into a bar...'
#Can be replaced up to 100 times
>>> setup.replace('a','b',100)
'b duck goes into b bbr...'
>>> setup.replace('a','b',2)
'b duck goes into b bar...'
>>> 60*60
3600
>>> seconds_per_hour=60*60
>>> seconds_per_hour*24
86400
>>> seconds_per_day=seconds_per_hour*24
>>> seconds_per_day/seconds_per_hour
24.0
It can be seen that the result is the same as 2-5.
>>> seconds_per_day//seconds_per_hour
24
Today we have finished Chapter 3. (Notes will be posted sequentially from tomorrow) I felt that lists, dictionaries, sets and tuples were important. I wasn't aware of it when I was a student. That may be the cause of the C stumbling block. Basically important.
"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"
Recommended Posts