山内セミナーⅠ(2022/06/08)

関連サイトと資料

サンプルプログラム - 文字列の操作

str_len = len('Hello World')
print(str_len)
msg = 'Good-bye World'
str_len = len(msg)
print(str_len)
    

msg = 'Hello'
print(msg[0])
print(msg[-1])
    

msg = 'Hello'
print(msg[1:4])
print(msg[:3])
print(msg[2:])
print(msg[::2])
    

msg = 'Hello ' "World"'''!
Good-bye'''""" World"""
print(msg)
    

world = 'World'
msg = 'Hello ' + world
print(msg)
    

some_value = '1'
result = int(some_value) + 1
print(result)
    

print('a' * 5)
    

print('Shimo-ogino' in 'Atsugi-shi, Shimo-ogino 1030, Kanagawa, Japan')
print('Tokyo' not in 'Atsugi-shi, Shimo-ogino 1030, Kanagawa, Japan')
    

address = '東京都世田谷区桜上水'
ward = '世田谷区'
  
if ward in address:
    print(address + 'は' + ward + 'にあります')
else:
    print(address + 'は' + ward + 'にはありません')
    

sample_str = 'find, rfind, index, rindex'
print(sample_str.find('index'))
print(sample_str.rfind('index'))
print(sample_str.find('foo'))
print(sample_str.index('find'))
print(sample_str.rindex('find'))
print(sample_str.index('foo'))
    

alpha_list = 'abc def ghi'.split()
print(alpha_list)
alpha_str = ','.join(alpha_list)
print(alpha_str)
    

data = 'abc, def, ghi'
data_list = data.split(',')
print(data_list)
    

sample_str = '   sample  '
print('begin:' + sample_str.strip() + ':end')
print('begin:' + sample_str.lstrip() + ':end')
print('begin:' + sample_str.rstrip() + ':end')
print(sample_str)
    

sample_str = '**++**sample**++**'
print(sample_str.strip('*'))
print(sample_str.strip('+*se'))
    

num = 'not a number!'
user_input = input('input number: ')
num = int(user_input)
print(num)
    

num = 'not a number'
user_input = input('input number: ')
if user_input.isdigit():
    num = int(user_input)
print(num)
    

sample_str = 'abc def GHI JKL'
print(sample_str.replace('abc', 'xyz'))
print(sample_str.swapcase())
print(sample_str.title())
print(sample_str.lower())
print(sample_str.upper())
    

sample_str = 'this is a sample string'
print(sample_str.startswith('this'))
print(sample_str.endswith('string'))
    

sample_str = 'this is a sample string'
print(sample_str.startswith(('that', 'those')))
print(sample_str.endswith(('sample', 'ing')))
    

sample_str = 'my_document.txt'
print(sample_str.removeprefix('my'))
print(sample_str.removesuffix('.txt'))
    

sample_str = 'my_document.txt'
print(sample_str.rstrip('.txt'))
    

sample_str = 'Python'
print(sample_str.ljust(12, '+'))
print(sample_str.center(12, '*'))
print(sample_str.rjust(12))
    

サンプルプログラム - 文字列の書式指定

x = 1
y = 100
result = 'x + y = {}'.format(x + y)
print(result)
    

x = 1
y = 100
result = '{} + {} = {}'.format(x, y, x + y)
print(result)
    

x = 1
y = 100
result = '{1} + {2} = {0}'.format(x + y, x, y)
print(result)
    

svo = {'subject': '私の名前は', 'verb': 'です', 'obj': '中野'}
print(svo['subject'] + svo['obj'] + svo['verb'])
    

svo = {'subject': '私の名前は', 'verb': 'です', 'object': '中野'}
result = '{0[subject]}{0[object]}{0[verb]}'.format(svo)
print(result)
    

svo = {'subject': '私の名前は', 'verb': 'です', 'object': '中野'}
result = '{svo[subject]}{svo[object]}{svo[verb]}'.format(svo=svo)
print(result)
    

x = 1
y = -2.1
result = '{x} + {y} = {result}'.format(result=x+y, x=x, y=y)
print(result)
    

x = 1
y = -2.1
result = '{x:8} + {y:8} = {result:8}'.format(result=x+y, x=x, y=y)
print(result)
    

x = 1
y = -2.1
result = '{x:<8} + {y:0=8} = {result:=8}'.format(result=x+y, x=x, y=y)
print(result)
    

x = 1
y = -2.1
result = '{x:<8} + {y:08} = {result:=8}'.format(result=x+y, x=x, y=y)
print(result)
    

x = 1
y = -2.1
result = '{x:< 8} + {y:0=8} = {result:=8}'.format(result=x+y, x=x, y=y)
print(result)
    

x = 10000
y = -4.5
result = '{x:8,} + {y:08} = {result:8_}'.format(result=x+y, x=x, y=y)
print(result)
    

y = 1234.56789
result = '{y:8.8}, {y:8.2}'.format(y=y)
print(result)
    

y = 1234.56789
result = '{y:8.3f}, {y:12.6f}, {y:8.3g}, {y:12.6g}'.format(y=y)
print(result)
    

x = 65536 * 2 + 128
print('{x:b}, {x:#b}, {x:#_b}'.format(x=x))
print('{x:o}, {x:#o}, {x:#_o}'.format(x=x))
print('{x:x}, {x:#x}, {x:#_x}'.format(x=x))
    

code_point = 65
f_number = -123.456789
percentage = 0.101
print('{cp:c}'.format(cp=code_point))
print('{num:e}, {num:.4e}'.format(num=f_number))
print('{p:%}, {p:.2%}'.format(p=percentage))
    

print(format(65, 'c'))
print(format(1234.567, '.2g'))
print(format(12.345, '0=+10'))
    

x = 1
y = 100
result = f'{x:0=+8} + {y:<8} = {x + y:$^8}'
print(result)
    

print('%d + %d = %d' % (1, 2, 1 + 2))
print('%#x' % 65535)
    

print('%(x)+.2e + %(y)+.2f = %(z)08.2f' % {'x': 1.1, 'y': -2.2, 'z': 1.1 + -2.2})