Pythonで文字列操作(len/要素取り出し/加算・乗算/検索)
Pythonで文字列操作で、文字長さの取得、要素取り出し、加算・乗算、文字列の中の検索(含まれているかどうかのみの確認)する方法です。
text = 'Hello Python'
#文字の長さを調べる
print(len(text))
#12
#1文字取り出し
print(text[1])
#e
#範囲指定で取り出し
print(text[1:5])
#ello
#指定箇所以降を取り出し
print(text[6:])
#Python
#2文字ごとに取り出し
print(text[::2])
#HloPto
#(加算)文字の結合
print(text+" World")
#Hello Python World
#(乗算)文字の繰り返し
print(text*3)
#Hello PythonHello PythonHello Python
#文字の検索(指定文字が含まれているか)
print('Python' in text)
#True
#文字の検索(指定文字が含まれていないか)
print('Python' not in text)
#False
[ad#ad-1]
スポンサーリンク