[Introduction to Python3 Day 4] Chapter 2 Py Components: Numbers, Strings, Variables (2.3.7-2.4)

Continued

2.3.7 Slicing by [start: end: step]

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]
''

2.3.8 Obtaining the length by len ()

The len () function counts the number of characters in a string.

>>> len(letters)
26
>>> empty=''
>>> len(empty)
0

2.3.9 Split by split ()

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']

2.3.10 Join by join ()

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

2.3.11 Various character string operations

>>> 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

2.3.12 Case sensitivity and placement


#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...'

2.3.13 Replace with replace ()

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...'

Review assignment

2-1 How many seconds is one hour?

>>> 60*60
3600

2-2,3 Let's assign the result of the previous question to a variable called seconds_per_hour. Also, how many seconds is a day?

>>> seconds_per_hour=60*60
>>> seconds_per_hour*24
86400

2-4,5 Calculate the number of seconds in a day, save it in the variable seconds_per_day, and divide by seconds_per_hour. Use floating point division (/).


>>> seconds_per_day=seconds_per_hour*24
>>> seconds_per_day/seconds_per_hour
24.0

2-6 Divide seconds_per_day by seconds_per_hour using integer division (//).

It can be seen that the result is the same as 2-5.


>>> seconds_per_day//seconds_per_hour
24

Impressions

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.

References

"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"

Recommended Posts

[Introduction to Python3 Day 3] Chapter 2 Py components: Numbers, strings, variables (2.2-2.3.6)
[Introduction to Python3 Day 2] Chapter 2 Py Components: Numbers, Strings, Variables (2.1)
[Introduction to Python3 Day 4] Chapter 2 Py Components: Numbers, Strings, Variables (2.3.7-2.4)
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
[Introduction to Python3 Day 7] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.3-3.8)
[Introduction to Python3 Day 5] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.1-3.2.6)
[Introduction to Python3 Day 6] Chapter 3 Py tool lists, tuples, dictionaries, sets (3.2.7-3.2.19)
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Introduction to Python3 Day 23] Chapter 12 Become a Paisonista (12.1 to 12.6)
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python3 Day 10] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.4-5.7)
[Introduction to Python3 Day 9] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.1-5.4)
[Introduction to Udemy Python3 + Application] 11. Character strings
Introduction to Effectiveness Verification Chapter 1 in Python
Introduction to effectiveness verification Chapter 3 written in Python
Introduction to Effectiveness Verification Chapter 2 Written in Python
Introduction to Python language
Introduction to OpenCV (python)-(2)
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
[Chapter 4] Introduction to Python with 100 knocks of language processing
Introduction to Python Django (2) Win
Introduction to serial communication [Python]
Display numbers and letters assigned to variables in python print
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 1
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
I read "Reinforcement Learning with Python: From Introduction to Practice" Chapter 2
[Introduction to Udemy Python 3 + Application] 31. Comments
Introduction to Python Numerical Library NumPy
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Udemy Python 3 + Application] 56. Closure
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Python] Let's use pandas
[Python] Convert natural numbers to ordinal numbers
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
Introduction to image analysis opencv python
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition