「Pythonの基礎 - 基本的なデータ型」の版間の差分

📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

文字列「</source>」を「</syntaxhighlight>」に置換
 
(同じ利用者による、間の4版が非表示)
6行目: 6行目:
以下のように、文字列リテラルをprint関数で出力するには()の中にダブルクォーテーション""で文字列を囲んで入力する。<br>
以下のように、文字列リテラルをprint関数で出力するには()の中にダブルクォーテーション""で文字列を囲んで入力する。<br>
ダブルクォーテーションの代わりにシングルクォーテーション''で囲んでもよい。<br>
ダブルクォーテーションの代わりにシングルクォーテーション''で囲んでもよい。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print("Python")
  print("Python")
   
   
  # 出力
  # 出力
  python
  python
  </source>
  </syntaxhighlight>
<br>
<br>
次に、以下のように入力すると、Python PHPと出力される。<br>
次に、以下のように入力すると、Python PHPと出力される。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print("Python", "PHP")
  print("Python", "PHP")
   
   
  # 出力
  # 出力
  Python PHP
  Python PHP
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


26行目: 26行目:
上記のように、複数の引数を並べて入力すると、文字列の間に半角の空白文字が1つ入る。<br>
上記のように、複数の引数を並べて入力すると、文字列の間に半角の空白文字が1つ入る。<br>
例えば、Python,PHPのように、間にカンマを入れて出力する場合は、sepオプションを使用する。<br>
例えば、Python,PHPのように、間にカンマを入れて出力する場合は、sepオプションを使用する。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print("Python", "PHP", sep=",")
  print("Python", "PHP", sep=",")
   
   
  # 出力
  # 出力
  Pyhon,PHP
  Pyhon,PHP
  </source>
  </syntaxhighlight>
<br>
<br>
===== endオプション =====
===== endオプション =====
endオプションは、文末の処理を行う時に使用する。通常は、標準で改行コード(\n)が設定されている。<br>
endオプションは、文末の処理を行う時に使用する。通常は、標準で改行コード(\n)が設定されている。<br>
明示的に入力すると以下のようになる。<br>
明示的に入力すると以下のようになる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print("Python", "PHP", sep=",", end="\n")
  print("Python", "PHP", sep=",", end="\n")
   
   
  # 出力
  # 出力
  Python,PHP
  Python,PHP
  </source>
  </syntaxhighlight>
<br>
<br>
次に、以下のように、endオプションを空にして実行すると、Python,PHPPython,PHPと改行されずに繋がって表示される。<br>
次に、以下のように、endオプションを空にして実行すると、Python,PHPPython,PHPと改行されずに繋がって表示される。<br>
これは、1行目は改行しないという意味になる。<br>
これは、1行目は改行しないという意味になる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print("Python", "PHP", sep=",", end="")
  print("Python", "PHP", sep=",", end="")
  print("Python", "PHP", sep=",", end="\n")
  print("Python", "PHP", sep=",", end="\n")
51行目: 51行目:
  # 出力
  # 出力
  Python,PHPPython,PHP
  Python,PHPPython,PHP
  </source>
  </syntaxhighlight>
<br>
<br>
また、endオプションには以下のような使い方もある。<br>
また、endオプションには以下のような使い方もある。<br>
最後の改行にエクスクラメーション(!)を入れて出力する場合は、end="!\n"のように入力する。
最後の改行にエクスクラメーション(!)を入れて出力する場合は、end="!\n"のように入力する。
  <source lang="python">
  <syntaxhighlight lang="python">
  print("Python", "PHP", sep=",", end="!\n")
  print("Python", "PHP", sep=",", end="!\n")
   
   
  # 出力
  # 出力
  Python,PHP!
  Python,PHP!
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


== 変数の宣言と使用 ==
== 変数の宣言と使用 ==
===== 基本 =====
===== 基本 =====
  <source lang="python">
  <syntaxhighlight lang="python">
  msg = "こんにちは、Python"
  msg = "こんにちは、Python"
  num = 100
  num = 100
74行目: 74行目:
  こんにちは、Python
  こんにちは、Python
  100
  100
  </source>
  </syntaxhighlight>
<br>
<br>
次にように、文字列リテラルを代入した変数msgに、数値リテラルを代入する場合、変数msgは自動的に整数型と認識される。<br>
次にように、文字列リテラルを代入した変数msgに、数値リテラルを代入する場合、変数msgは自動的に整数型と認識される。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  msg = "こんにちは、Python"
  msg = "こんにちは、Python"
  num = 100
  num = 100
85行目: 85行目:
  # 出力
  # 出力
  100
  100
  </source>
  </syntaxhighlight>
<br>
<br>
Pythonでは、変数を宣言する時、データ型を自動的に認識するので型宣言は不要である。(これを動的型付けと呼ぶ)<br>
Pythonでは、変数を宣言する時、データ型を自動的に認識するので型宣言は不要である。(これを動的型付けと呼ぶ)<br>
ただし、型宣言があると理解しやすい時もあるので、以下のように、変数の型も併せて記述する方法もある。<br>
ただし、型宣言があると理解しやすい時もあるので、以下のように、変数の型も併せて記述する方法もある。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  msg: str = "こんにちは、Python"
  msg: str = "こんにちは、Python"
  num: int = 100
  num: int = 100
  print(msg)
  print(msg)
  print(num)
  print(num)
  </source>
  </syntaxhighlight>
<br>
<br>
変数の型を知りたい場合、type関数を使用してデータ型を表示させることができる。<br>
変数の型を知りたい場合、type関数を使用してデータ型を表示させることができる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  msg = "こんにちは、Python"
  msg = "こんにちは、Python"
  num = 100
  num = 100
106行目: 106行目:
  こんにちは、Python <class 'str'>
  こんにちは、Python <class 'str'>
  100 <class 'int'>
  100 <class 'int'>
  </source>
  </syntaxhighlight>
<br>
<br>
===== 型変換(キャスト) =====
===== 型変換(キャスト) =====
文字列型の変数strnumを整数型の変数inumに代入する場合、以下のように型変換を行うと、整数型の100として表示される。<br>
文字列型の変数strnumを整数型の変数inumに代入する場合、以下のように型変換を行うと、整数型の100として表示される。<br>
ただし、数値ではない文字列を数値型に変換することはできない。<br>
ただし、数値ではない文字列を数値型に変換することはできない。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  strnum = "100"
  strnum = "100"
  inum = int(strnum)
  inum = int(strnum)
118行目: 118行目:
  # 出力
  # 出力
  100 <class 'int'>
  100 <class 'int'>
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


133行目: 133行目:
このように、文字列は順序を持つ型なのでシーケンス型のひとつになっている。<br>
このように、文字列は順序を持つ型なのでシーケンス型のひとつになっている。<br>
<br>
<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print('Hello')
  print('Hello')
  print("Hello")
  print("Hello")
149行目: 149行目:
  Hello Python!
  Hello Python!
  13
  13
  </source>
  </syntaxhighlight>
<br>
<br>
===== 改行 =====
===== 改行 =====
改行する箇所に、\nを入れる。<br>
改行する箇所に、\nを入れる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print("Hello, \nPython")
  print("Hello, \nPython")
   
   
159行目: 159行目:
  Hello,  
  Hello,  
  Python
  Python
  </source>
  </syntaxhighlight>
<br>
<br>
複数行にわたる長い文章の表示は、トリプルクォート(""" """)で囲む。<br>
複数行にわたる長い文章の表示は、トリプルクォート(""" """)で囲む。<br>
トリプルクォートで囲んだ部分は、改行も自由にできる。<br>
トリプルクォートで囲んだ部分は、改行も自由にできる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  strhoge = """Hello,  
  strhoge = """Hello,  
               Python"""
               Python"""
171行目: 171行目:
  Hello,  
  Hello,  
  Python
  Python
  </source>
  </syntaxhighlight>
<br>
<br>
文字列が長くなって読みにくい場合は、丸括弧()の中で改行する文字列ごとにダブルクォーテーションで囲むか、<br>
文字列が長くなって読みにくい場合は、丸括弧()の中で改行する文字列ごとにダブルクォーテーションで囲むか、<br>
改行する場所でバックスラッシュ\を入れることでコードを途中で改行して見やすくすることができる。<br>
改行する場所でバックスラッシュ\を入れることでコードを途中で改行して見やすくすることができる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  str1 = ("Hello, "
  str1 = ("Hello, "
         "Python")
         "Python")
181行目: 181行目:
  str2 = "Hello, "\
  str2 = "Hello, "\
         "Python"
         "Python"
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


202行目: 202行目:
文字列型の変数の後ろに[]を使用して、その中にインデックスを指定する。
文字列型の変数の後ろに[]を使用して、その中にインデックスを指定する。
例えば、以下のように、変数wordの後ろに[]をつけて抽出する文字列のインデックスを指定する。
例えば、以下のように、変数wordの後ろに[]をつけて抽出する文字列のインデックスを指定する。
  <source lang="python">
  <syntaxhighlight lang="python">
  word = "python"
  word = "python"
  print(word[0])
  print(word[0])
214行目: 214行目:
  n
  n
  o
  o
  </source>
  </syntaxhighlight>
<br>
<br>
===== 文字列の抽出 =====
===== 文字列の抽出 =====
221行目: 221行目:
  [start:end:step]
  [start:end:step]
<br>
<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  word = "python"
  word = "python"
  print(word[0:3])
  print(word[0:3])
244行目: 244行目:
  agmsy
  agmsy
  cgkos
  cgkos
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


== 文字列の検索・置換・結合 ==
== 文字列の検索・置換・結合 ==
===== len関数で文字列の長さを出力 =====
===== len関数で文字列の長さを出力 =====
  <source lang="python">
  <syntaxhighlight lang="python">
  s = "I am playing with Python. It is not a serpent Python."
  s = "I am playing with Python. It is not a serpent Python."
  print(len(s))
  print(len(s))
255行目: 255行目:
  # 出力
  # 出力
  53
  53
  </source>
  </syntaxhighlight>
<br>
<br>
===== in演算子を使用して存在の有無を調べる ==
===== in演算子を使用して存在の有無を調べる =====
文字列の中に、特定の文字や文字列が含まれるかどうかをin演算子を使用して調べる。<br>
文字列の中に、特定の文字や文字列が含まれるかどうかをin演算子を使用して調べる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  s = "I am playing with Python. It is not a serpent Python."
  s = "I am playing with Python. It is not a serpent Python."
  in_python_word = "Python" in s
  in_python_word = "Python" in s
270行目: 270行目:
  True
  True
  False
  False
  </source>
  </syntaxhighlight>
<br>
<br>
===== split関数で文字列を分割する =====
===== split関数で文字列を分割する =====
split関数を引数なしで使用すると、セパレータとして空白文字(スペース区切り)が設定される。<br>
split関数を引数なしで使用すると、セパレータとして空白文字(スペース区切り)が設定される。<br>
出力には、それぞれの単語ごとに分割されて、角括弧[]の中(リスト形式の表現)に入れられる。<br>
出力には、それぞれの単語ごとに分割されて、角括弧[]の中(リスト形式の表現)に入れられる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  s = "I am playing with Python. It is not a serpent Python."
  s = "I am playing with Python. It is not a serpent Python."
  print(s.split())
  print(s.split())
281行目: 281行目:
  # 出力
  # 出力
  ['I', 'am', 'playing', 'with', 'Python.', 'It', 'is', 'not', 'a', 'serpent', 'Python.']
  ['I', 'am', 'playing', 'with', 'Python.', 'It', 'is', 'not', 'a', 'serpent', 'Python.']
  </source>
  </syntaxhighlight>
<br>
<br>
次に、セパレータにピリオドを指定して分割する。<br>
次に、セパレータにピリオドを指定して分割する。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  s = "I am playing with Python. It is not a serpent Python."
  s = "I am playing with Python. It is not a serpent Python."
  print(s.split("."))
  print(s.split("."))
290行目: 290行目:
  # 出力
  # 出力
  ['I am playing with Python'. ' It is not a serpent Python']
  ['I am playing with Python'. ' It is not a serpent Python']
  </source>
  </syntaxhighlight>
<br>
<br>
更に、セパレータにtを指定して分割する。<br>
更に、セパレータにtを指定して分割する。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  s = "I am playing with Python. It is not a serpent Python."
  s = "I am playing with Python. It is not a serpent Python."
  print(s.split("t"))
  print(s.split("t"))
299行目: 299行目:
  # 出力
  # 出力
  ['I am playing wi', 'h Py', 'hon. I', ' is no', ' a serpen', ' Py', 'hon']
  ['I am playing wi', 'h Py', 'hon. I', ' is no', ' a serpen', ' Py', 'hon']
  </source>
  </syntaxhighlight>
<br>
<br>
===== join関数で結合する =====
===== join関数で結合する =====
join関数は、split関数とは逆の関数で、文字列のリストを1つの文字列に結合する。<br>
join関数は、split関数とは逆の関数で、文字列のリストを1つの文字列に結合する。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  s = ['I am playing wi', 'h Py', 'hon. I', ' is no', ' a serpen', ' Py', 'hon.']
  s = ['I am playing wi', 'h Py', 'hon. I', ' is no', ' a serpen', ' Py', 'hon.']
  t = "t"
  t = "t"
310行目: 310行目:
  # 出力
  # 出力
  I am playing with Python. It is not a serpent Python.
  I am playing with Python. It is not a serpent Python.
  </source>
  </syntaxhighlight>
<br>
<br>
===== find関数で検索する =====
===== find関数で検索する =====
find関数は、対象の文字列から検索したい文字列がどこにあるのか調べる関数である。<br>
find関数は、対象の文字列から検索したい文字列がどこにあるのか調べる関数である。<br>
以下の例では、Pythonという文字列が先頭から数えて18番目のインデックスにあるという意味になる。<br>
以下の例では、Pythonという文字列が先頭から数えて18番目のインデックスにあるという意味になる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  s = "I am playing with Python. It is not a serpent Python."
  s = "I am playing with Python. It is not a serpent Python."
  print(s.find("Python"))
  print(s.find("Python"))
321行目: 321行目:
  # 出力
  # 出力
  18
  18
  </source>
  </syntaxhighlight>
<br>
<br>
また、文字列を後ろから検索する場合は、rfind関数を使用する。<br>
また、文字列を後ろから検索する場合は、rfind関数を使用する。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  s = "I am playing with Python. It is not a serpent Python."
  s = "I am playing with Python. It is not a serpent Python."
  print(s.rfind("Python"))
  print(s.rfind("Python"))
330行目: 330行目:
  # 出力
  # 出力
  46
  46
  </source>
  </syntaxhighlight>
<br>
<br>
===== count関数で数える =====
===== count関数で数える =====
len関数は全体の個数を数えることができるが、文字列の中に特定の文字列を数えるにはcount関数を使用する。<br>
len関数は全体の個数を数えることができるが、文字列の中に特定の文字列を数えるにはcount関数を使用する。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  s = "I am playing with Python. It is not a serpent Python."
  s = "I am playing with Python. It is not a serpent Python."
  print(s.count("Python"))
  print(s.count("Python"))
342行目: 342行目:
  2
  2
  5
  5
  </source>
  </syntaxhighlight>
<br>
<br>
===== replace関数で置換する =====
===== replace関数で置換する =====
文字や文字列を置き換える場合は、replace関数を使用する。<br>
文字や文字列を置き換える場合は、replace関数を使用する。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  s = "I am playing with Python."
  s = "I am playing with Python."
  print(s.replace("Python", "Swift"))
  print(s.replace("Python", "Swift"))
352行目: 352行目:
  # 出力
  # 出力
  I am playing with Swift.
  I am playing with Swift.
  </source>
  </syntaxhighlight>
<br>
<br>


===== capitalize関数、title関数、upper関数、lower関数で文字列を変換する ==
===== capitalize関数、title関数、upper関数、lower関数で文字列を変換する =====
これらの関数は、文字列の大文字・小文字を操作する時に使用する。<br>
これらの関数は、文字列の大文字・小文字を操作する時に使用する。<br>
* capitalize()は先頭の単語をタイトルケースに変換する。
* capitalize()は先頭の単語をタイトルケースに変換する。
361行目: 361行目:
* upper()は全ての文字を大文字に変換する。
* upper()は全ての文字を大文字に変換する。
* lower()は全ての文字を小文字に変換する。
* lower()は全ての文字を小文字に変換する。
  <source lang="python">
  <syntaxhighlight lang="python">
  s = "I am playing with Python. It is not a serpent Python."
  s = "I am playing with Python. It is not a serpent Python."
  print(s.capitalize())
  print(s.capitalize())
373行目: 373行目:
  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>
  </syntaxhighlight>
<br><br>
<br><br>


__FORCETOC__
__FORCETOC__
[[カテゴリ:Python]]
[[カテゴリ:Python]]