A little review content will be included
It is a method to change the shape as a simple error.
#String conversion
year = str(year)
#By the way, number conversion
year = int(year)
You can put characters in {} in the text with .format.
#Substitute the number 20 for the variable age
age = 20
#In the variable msg,"I,{}I'm old."Substitute the string
msg = "I,{}I'm old."
#Using the variables msg and age"I am 20 years old."Output
print(msg.format(age))
#Output result
I am 20 years old.
With format (), in the order of the arguments Sequentially assigned in {}.
print("I,{}From{}I went on a trip.".format("Tokyo", "Osaka"))
#Output result
I went on a trip from Tokyo to Osaka.
For format, if you put a number in {}, in that order Will be substituted.
For list type, specify the index number with [].
print("my name is,{0} {1}is.".format("Ishikawa", "Akihiko"))
#Output result
"My name is Ishikawa Akihiko."
print("my name is,{1} {0}is.".format("Ishikawa", "Akihiko"))
#Output result
"My name is Akihiko Ishikawa."
#Str the list type object.format()When taking as an argument of a function
#Specify as follows. Square brackets[]Numbers with are indicating the order in the list
#The number without it indicates the number of the argument of the format function.
list = ["Ishikawa", "Akihiko"]
"my name is,{0[0]} {0[1]}",format(list)
=> "My name is Ishikawa Akihiko."
In the case of dictionary type, the value is the second argument. In other words It will be in the form of {1 []}.
dic = {
"Night view" : ["Kobe", "Hakodate", "Nagasaki"],
"Festival" : ["祇園Festival", "天神Festival", "神田Festival"],
"Famous waterfall" : ["Kegon Falls", "Nachi Falls", "Fukuroda Falls"]
}
template = "In general, Japan's three major{0}Is "{1[0]}」、「{1[1]}」、「{1[2]}Refers to."
#Output the contents of the variable dic in a format like (example).
for key in dic:
print(template.format(key,dic[key]))
#Output result
Generally, the three major night views of Japan are "Kobe", "Hakodate", and "Nagasaki".
Generally, the three major festivals in Japan are "Gion Matsuri", "Tenjin Matsuri", and "Kanda Matsuri".
In general, the three major waterfalls in Japan are "Kegon Falls," "Nachi Falls," and "Fukuroda Falls."
Next, as with the dictionary type handling, it is a method to specify the key and retrieve it.
dic = {
"CEO" : {"Ai" : "Ishikawa Akihiko", "Apple" : "Tim Cook", "Facebook" : "Mark Zuckerberg" },
"location" : {"Ai" : "Japan", "Apple" : "America", "Facebook" : "America"},
"founded_year" : {"Ai" : 2014, "Apple" : 1976, "Facebook" : 2004}
}
#Output the contents of the variable dic in a format like (example).
for key in dic:
print("{0} Ai : {1[Ai]} Apple : {1[Apple]} Facebook : {1[Facebook]}".format(key, dic[key]))
#Output result
CEO Ai : Ishikawa Akihiko Apple : Tim Cook Facebook : Mark Zuckerberg
location Ai :Japan Apple:America Facebook:America
founded_year Ai : 2014 Apple : 1976 Facebook : 2004
You can freely change the arrangement of the character strings using the str.format () function. The str.format () function uses a colon: inside the curly braces {} Options can be specified. For example, if you want to specify centering for 20 characters, write {: ^ 20}.
#When centering
"{:^20}".format("abcde")
#When left justified
"{:<20}".format("abcde")
#When right-aligned
"{:>20}".format("abcde")
dic = {
"CEO" : {"Ai" : "Ishikawa Akihiko", "Apple" : "Tim Cook", "Facebook" : "Mark Zuckerbarg" },
"location" : {"Ai" : "Japan", "Apple" : "America", "Facebook" : "America"},
"founded_year" : {"Ai" : 2014, "Apple" : 1976, "Facebook" : 2004}
}
# str.format()Align the character widths by specifying function options.
for key in dic:
print("{0:^20} Ai : {1[Ai]:^20} Apple : {1[Apple]:^20} Facebook : {1[Facebook]:^20}".format(key, dic[key]))
#Output result
CEO Ai : Ishikawa Akihiko Apple : Tim Cook Facebook : Mark Zuckerbarg
location Ai : Japan Apple : America Facebook : America
founded_year Ai : 2014 Apple : 1976 Facebook : 2004
.split ("") Specifies the point to split.
url = "http://blog.ai.net"
#The variable url is delimited by dots (`"."`) As str.split()Split by function and variable split_Substitute it in the url.
split_url=url.split(".")
# split_Please output the url.
print(split_url)
#Split split_Extract the last stored character string from the url and output it.
print(split_url[-1])
#Output result
['http://blog', 'ai', 'net']
net
Specify where and the list to join with "". join ().
list = ["2000", "12", "31"]
"-".join(list)
print(list)
#Output result
'2000-12-31'
When you want to save the execution result of the program to a file When you want to handle the data acquired from the web programmatically You need to treat the file as an object.
Use the open function to get the file object.
open(file name,mode)
Now you can get the object of the file specified by the file name.
There are various modes such as "w" (overwrite) or "a" (additional).
mode | Contents |
---|---|
"w" | When you want to write data from a program to a file |
"a" | When you want to write data from a program to a file |
"r" | If you want to read data from a file into a program |
"r+" | If you want to write and read |
#Mode too""Don't forget to enclose it in.
# "hello.txt"Open write-only and assign it to the variable file1.
file1=open("hello.txt","w")
# "test.txt"Open read-only and assign it to the variable file2.
file2 = open("test.txt","r")
When a file object called f is open
f.close() #You can now close the file.
When you open a file with the open function, you should make a habit of closing it with the close function.
Suppose the open function opens a file object called f.
When you want to write characters to a file from a program
f.write("The character string you want to write") #You can write by using this.
When opened in write-only mode ("w"), the characters in the write function overwrite the contents of the file.
To programmatically read data from a text file that already contains content Use the read function. If a read-only open file object f exists, do the following:
f.read() #Specify like this.
f.readline() #If you want to read only one line, specify as.
You can operate the file more easily by using the with syntax.
#When reading from a file
with open("file name", "r") as f:
f.read()
#When writing to a file
with open("file name", "w") as f:
f.write("What you want to write")
If you write using with, the file will be closed automatically even if a problem occurs in the with block. There is no need to use the close function. To manage file resources safely Make sure to use with to open the file.
Recommended Posts