point
s = 'Hello, world.'
print(str(s))
# Hello, world.
print(repr(s))
# 'Hello, world.'
print(str(1/7))
# 0.14285714285714285
x = 10 * 3.25
y = 200 * 200
s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
print(s)
# The value of x is 32.5, and y is 40000...
#String repr()Returns string quotes and backslashes as is
hello = 'hello, world\n'
hellos = repr(hello)
print(hellos)
# 'hello, world\n'
#Every Python object is a repr()Can be converted with
print(repr((x, y, ('spam', 'eggs'))))
# (32.5, 40000, ('spam', 'eggs'))
str.rjust ()
ljust ()
, center ()
rjust()Right justification using
for x in range(1, 11):
print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
print(repr(x*x*x).rjust(4))
#output
# 1 1 1
# 2 4 8
# 3 9 27
# 4 16 64
# 5 25 125
# 6 36 216
# 7 49 343
# 8 64 512
# 9 81 729
# 10 100 1000
Right justification using format
for x in range(1, 11):
print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
#output
# 1 1 1
# 2 4 8
# 3 9 27
# 4 16 64
# 5 25 125
# 6 36 216
# 7 49 343
# 8 64 512
# 9 81 729
# 10 100 1000
zfill ()
zero-pads the left side of the stringprint('12'.zfill(5))
# 00012
print('-3.14'.zfill(7))
# -003.14
print('3.14159265359'.zfill(5))
# 3.14159265359
str.format ()
replaces {}
in the string with the argument value{1}
{arg}
Various formats
#in format{}use
print('Happy birth day {} !, You are {} years old now !'.format('Kawauso', '5'))
# Happy birth day Kawauso !, You are 5 years old now !
#Argument numbering
print('Happy birth day {0} !, You are {1} years old now !'.format('Kawauso', '5'))
# Happy birth day Kawauso !, You are 5 years old now !
#Keyword argument specification
print('Happy birth day {name} !, You are {year} years old now !'.format(name='Kawauso', year='5'))
# Happy birth day Kawauso !, You are 5 years old now !
#Mixed number and keyword arguments
print('Happy birth day {0} !, You are {year} years old now !'.format('Kawauso', year='5'))
# Happy birth day Kawauso !, You are 5 years old now !
#float type notation
print('Happy birth day {name} !, You are {year:3f} now !'.format(name='Kawauso', year=5))
# Happy birth day Kawauso !, You are 5.000000 now !
#When r is used, repr()Is applied
print('Happy birth day Kawauso !, You are {!r} now !'.format(5.12345678))
# Happy birth day Kawauso !, You are 5.12345678 now !
#Notated with 3 digits after the decimal point
import math
print('Pi is about{0:.3f}Is'.format(math.pi))
#Pi is about 3.142
# %How to specify using
print('Pi is about%5.3f' % math.pi)
#Pi is about 3.142
# :You can specify the minimum width of the number of characters in the field by passing an integer after
table = {'kawauso': 100, 'mando': 200, 'banjo': 300}
for name, num in table.items():
print('{0:10} ==> {1:10d}'.format(name, num))
# kawauso ==> 100
# mando ==> 200
# banjo ==> 300
#Pass the dict[]It is convenient to access by name because you can specify it with
print('kawauso: {0[kawauso]:d}; mando: {0[mando]:d}; '
'banjo: {0[banjo]}'.format(table))
# kawauso: 100; mando: 200; banjo: 300
# **You can do the same by passing it as a keyword argument using notation
print('kawauso: {kawauso:d}; mando: {mando:d}; '
'banjo: {banjo}'.format(**table))
# kawauso: 100; mando: 200; banjo: 300
open()
f = open('file', 'w')
\ n
in text mode\ n
to platform-dependent line endingsOperate on this text file
workfile
The first line
2nd line
3rd line
f.read()
f = open('workfile', 'r')
print(f.read())
#output
#The first line
#2nd line
#3rd line
f.close()
f.readline()
print(f.readline())
print(f.readline())
#output
#The first line
#
#2nd line
for line in f:
print(line, end='')
#output
#The first line
#
#2nd line
#
#3rd line
# f.write('4th line')
# print(f.read())
f.readlines()
print(f.readlines())
#output
# ['The first line\n', '2nd line\n', '3rd line']
f.write()
print(f.write('4th line'))
#output
# 3
f.tell()
f = open('workfile', 'rb+')
print(f.write(b'0123456789abscef'))
# 16
print(f.seek(5))
# 5
print(f.read(1))
# b'5'
print(f.seek(-3, 2))
# 16
print(f.read(1))
# b's'
f.close()
with open('workfile', 'r') as f:
read_data = f.read()
print(f.closed)
# True
point
import json
x = [1, 'kawasuo', 'list']
print(json.dumps(x))
# [1, "kawasuo", "list"]
with open('sample.json', 'w') as f:
# sample.Write to json
json.dump(x, f)
with open('sample.json', 'r') as f:
# sample.Read from json
print(json.load(f))
# [1, "kawasuo", "list"]
Recommended Posts