13,005
回編集
(ページの作成:「== 概要 == ここでは、Pythonプログラミングの基礎として、基本的な文法等を記載する。<br> <br><br> == print関数 == 以下のように、…」) |
(→文字列) |
||
181行目: | 181行目: | ||
str2 = "Hello, "\ | str2 = "Hello, "\ | ||
"Python" | "Python" | ||
</source> | |||
<br><br> | |||
== 文字列の抽出と分割 == | |||
文字列は先頭の文字のインデックスを0として順番にインデックスが割り当てられている。<br> | |||
例えば、文字列pythonにインデックスを振ると次のようになる。<br> | |||
<center> | |||
{| class="wikitable" | |||
|- | |||
! p !! y !! t !! h !! o !! n | |||
|- | |||
| 0 || 1 || 2 || 3 || 4 || 5 | |||
|- | |||
| -6 || -5 || -4 || -3 || -2 || -1 | |||
|} | |||
</center> | |||
<br> | |||
===== 文字の抽出 ===== | |||
インデックスを使用して、文字列の中から文字を抽出する。 | |||
文字列型の変数の後ろに[]を使用して、その中にインデックスを指定する。 | |||
例えば、以下のように、変数wordの後ろに[]をつけて抽出する文字列のインデックスを指定する。 | |||
<source lang="python"> | |||
word = "python" | |||
print(word[0]) | |||
print(word[1]) | |||
print(word[-1]) | |||
print(word[-2]) | |||
# 出力 | |||
p | |||
y | |||
n | |||
o | |||
</source> | |||
<br> | |||
===== 文字列の抽出 ===== | |||
文字列を抽出する場合は、コロンを使用して分割する範囲を指定する。<br> | |||
startの位置に抽出する文字の先頭のインデックスを指定し、endの位置にend - 1番目のインデックスを指定する。stepは、その文字数毎に抜き出すという意味になる。<br> | |||
[start:end:step] | |||
<br> | |||
<source lang="python"> | |||
word = "python" | |||
print(word[0:3]) | |||
print(word[:3]) | |||
print(word[2:5]) | |||
print(word[2:]) | |||
print(word[-2:]) | |||
print(word[-4:-2]) | |||
words = "abcdefghijklmnopqrstuvwxyz" | |||
print(words[::6]) | |||
print(words[2:21:4]) | |||
# 出力 | |||
pyt | |||
pyt | |||
tho | |||
thon | |||
on | |||
th | |||
agmsy | |||
cgkos | |||
</source> | |||
<br><br> | |||
== 文字列の検索・置換・結合 == | |||
===== len関数で文字列の長さを出力 ===== | |||
<source lang="python"> | |||
s = "I am playing with Python. It is not a serpent Python." | |||
print(len(s)) | |||
# 出力 | |||
53 | |||
</source> | |||
<br> | |||
===== in演算子を使用して存在の有無を調べる == | |||
文字列の中に、特定の文字や文字列が含まれるかどうかをin演算子を使用して調べる。<br> | |||
<source lang="python"> | |||
s = "I am playing with Python. It is not a serpent Python." | |||
in_python_word = "Python" in s | |||
in_php_word = "PHP" in s | |||
print(in_python_word) | |||
print(in_php_word) | |||
# 出力 | |||
True | |||
False | |||
</source> | |||
<br> | |||
===== split関数で文字列を分割する ===== | |||
split関数を引数なしで使用すると、セパレータとして空白文字(スペース区切り)が設定される。<br> | |||
出力には、それぞれの単語ごとに分割されて、角括弧[]の中(リスト形式の表現)に入れられる。<br> | |||
<source lang="python"> | |||
s = "I am playing with Python. It is not a serpent Python." | |||
print(s.split()) | |||
# 出力 | |||
['I', 'am', 'playing', 'with', 'Python.', 'It', 'is', 'not', 'a', 'serpent', 'Python.'] | |||
</source> | |||
<br> | |||
次に、セパレータにピリオドを指定して分割する。<br> | |||
<source lang="python"> | |||
s = "I am playing with Python. It is not a serpent Python." | |||
print(s.split(".")) | |||
# 出力 | |||
['I am playing with Python'. ' It is not a serpent Python'] | |||
</source> | |||
<br> | |||
更に、セパレータにtを指定して分割する。<br> | |||
<source lang="python"> | |||
s = "I am playing with Python. It is not a serpent Python." | |||
print(s.split("t")) | |||
# 出力 | |||
['I am playing wi', 'h Py', 'hon. I', ' is no', ' a serpen', ' Py', 'hon'] | |||
</source> | |||
<br> | |||
===== join関数で結合する ===== | |||
join関数は、split関数とは逆の関数で、文字列のリストを1つの文字列に結合する。<br> | |||
<source lang="python"> | |||
s = ['I am playing wi', 'h Py', 'hon. I', ' is no', ' a serpen', ' Py', 'hon.'] | |||
t = "t" | |||
print(t.join(s)) | |||
# 出力 | |||
I am playing with Python. It is not a serpent Python. | |||
</source> | |||
<br> | |||
===== find関数で検索する ===== | |||
find関数は、対象の文字列から検索したい文字列がどこにあるのか調べる関数である。<br> | |||
以下の例では、Pythonという文字列が先頭から数えて18番目のインデックスにあるという意味になる。<br> | |||
<source lang="python"> | |||
s = "I am playing with Python. It is not a serpent Python." | |||
print(s.find("Python")) | |||
# 出力 | |||
18 | |||
</source> | |||
<br> | |||
また、文字列を後ろから検索する場合は、rfind関数を使用する。<br> | |||
<source lang="python"> | |||
s = "I am playing with Python. It is not a serpent Python." | |||
print(s.rfind("Python")) | |||
# 出力 | |||
46 | |||
</source> | |||
<br> | |||
===== count関数で数える == | |||
len関数は全体の個数を数えることができるが、文字列の中に特定の文字列を数えるにはcount関数を使用する。<br> | |||
<source lang="python"> | |||
s = "I am playing with Python. It is not a serpent Python." | |||
print(s.count("Python")) | |||
print(s.count("n")) | |||
# 出力 | |||
2 | |||
5 | |||
</source> | |||
<br> | |||
===== replace関数で置換する ===== | |||
文字や文字列を置き換える場合は、replace関数を使用する。<br> | |||
<source lang="python"> | |||
s = "I am playing with Python." | |||
print(s.replace("Python", "Swift")) | |||
# 出力 | |||
I am playing with Swift. | |||
</source> | |||
<br> | |||
===== capitalize関数、title関数、upper関数、lower関数で文字列を変換する == | |||
これらの関数は、文字列の大文字・小文字を操作する時に使用する。<br> | |||
* capitalize()は先頭の単語をタイトルケースに変換する。 | |||
* title()は全ての単語をタイトルケースに変換する。 | |||
* upper()は全ての文字を大文字に変換する。 | |||
* lower()は全ての文字を小文字に変換する。 | |||
<source lang="python"> | |||
s = "I am playing with Python. It is not a serpent Python." | |||
print(s.capitalize()) | |||
print(s.title()) | |||
print(s.upper()) | |||
print(s.lower()) | |||
# 出力 | |||
I am playing with python. it is not a serpent python. | |||
I Am Playing With Python. It Is Not A Serpent Python. | |||
I AM PLAYING WITH PYTHON. IT IS NOT A SERPENT PYTHON. | |||
i am playing with python. it is not a serpent python. | |||
</source> | </source> | ||
<br><br> | <br><br> |