Working with strings · 文字列の操作
Letters have positions
- A string is a row of characters.
- Each character has a position, called an index.
- Indexes start at
0. Uses[-1]for the last character.
文字には位置がある
- ストリングは文字の並びです。
- 各文字にはインデックスと呼ばれる位置があります。
- インデックスは
0から始まります。最後の文字にはs[-1]を使用します。
word = "Python"
print(word[0])
print(word[-1])
Take a slice
- A slice takes part of a string:
s[start:end]. - The character at
endis not included. - Leave a side empty for "from the start" or "to the end":
s[:3],s[2:].
スライスで部分を取り出す
- スライスはストリングの一部を取り出します:
s[start:end]。 endの位置にある文字は含まれません。- 「先頭から」や「終端まで」を指定するには片方を空にします:
s[:3],s[2:]。
msg = "hello"
print(msg[:3])
print(msg[2:])
Worked example: positions and slices
- In
"banana"the positions areb=0,a=1,n=2,a=3,n=4,a=5. word[0]is the first letter;word[-1]counts back from the end.word[:3]takes positions 0, 1 and 2, and stops before 3.
worked example: 位置とスライス
"banana"における位置はb=0,a=1,n=2,a=3,n=4,a=5 です。word[0]は最初の文字です;word[-1]は末尾から数えます。word[:3]は位置 0, 1, 2 を取り出し、3 に達する前に停止します。
word = "banana"
print(word[0]) # b
print(word[-1]) # a (the last letter)
print(word[:3]) # ban (positions 0, 1, 2)
print(word[2:5]) # nan (positions 2, 3, 4)
Length and changing case
len(s)gives the number of characters.s.upper()makes a CAPITAL copy;s.lower()makes a lowercase copy.- These make a new string; the original does not change.
長さケースの変更
len(s)は文字の総数を返します。s.upper()は大文字のコピーを作成し、s.lower()は小文字のコピーを作成します。- これらは新しいストリングを作成するため、元のストリングは変わりません。
city = "Tokyo"
print(len(city))
print(city.upper())
print(city.lower())
f-strings build text
- An f-string starts with
fbefore the quote. - Put a value inside
{ }and Python drops it into the text. - It is the neat way to mix words and values.
f-stringsはテキストを構成する
- f-string はクォート直前に
fで始めます。 { }内に値を入れると、Pythonがそれをテキストに埋め込みます。- 単語と値を混ぜるための簡潔な方法です。
fruit = "apple"
count = 3
print(f"I have {count} {fruit}s")
Common mistakes
- Indexing starts at
0;s[-1]is the last character. - Strings cannot be changed in place — methods like
.upper()return a NEW string, so store the result. - You can only join a string to a string with
+.
よくあるミス
- インデックス化は
0から始まり、s[-1]は最後の文字です。 - ストリングはインプレイスでは変更できません。
.upper()などのメソッドは新しいストリングを返すため、結果を保存してください。 +でストリング同士しか結合できません。
Now you try
- Read the task to see which variable to fill or what to print.
- Press Check answer to test your code.
あなたも試してみよう
- タスクを読み取って、どの変数を埋めるべきか、あるいは何を出力すべきかを把握してください。
- 回答を確認 を押してコードを試してください。
Index & slice a string · 文字列のインデックスとスライス
Each character has an index from 0; a slice s[a:b] takes a range. · 各文字には0からのインデックスがあります。スライス s[a:b] は範囲を取得します。
word is "computer". Store its first character in first. Indexes start at 0, so that character is word[0]. · word は "computer" です。その最初の文字をfirst に格納してください。インデックスは0から始まるので、その文字のインデックスは word[0] です。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
first is already the first character of "computer". Store the first four characters in first_four with a slice. word[a:b] stops before b, so four characters is word[:4]. · first は既に "computer" の最初の文字です。スライスを使って最初の4文字をfirst_four に格納してください。word[a:b] は b より前に止まるため、4文字分は word[:4] です。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
phrase is "Hello World". Store its length in n with len(...), and an UPPERCASE copy in loud with .upper(). · phrase は "Hello World" です。長さをn にlen(...) で格納し、大文字のコピーをloud に.upper() で格納してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
name is "Mia" and age is 12. Use an f-string to print exactly: Mia is 12 years old. · name は "Mia" で age は 12 です。f-stringを使って以下を正確に出力してください:Mia is 12 years old.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
first is "ada" and last is "lovelace". Store the initials in initials as capital letters, each followed by a dot: "A.L.". · first は "ada" で last は "lovelace" です。イニシャルをinitials に大文字で格納し、それぞれドットを付けてください:"A.L."。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。