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

ナビゲーションに移動 検索に移動
文字列「source lang」を「syntaxhighlight lang」に置換
(Wiki がページ「基本的なデータ型(Python)」を「Pythonの基礎 - 基本的なデータ型」に、リダイレクトを残さずに移動しました)
(文字列「source lang」を「syntaxhighlight lang」に置換)
6行目: 6行目:
以下のように、文字列リテラルをprint関数で出力するには()の中にダブルクォーテーション""で文字列を囲んで入力する。<br>
以下のように、文字列リテラルをprint関数で出力するには()の中にダブルクォーテーション""で文字列を囲んで入力する。<br>
ダブルクォーテーションの代わりにシングルクォーテーション''で囲んでもよい。<br>
ダブルクォーテーションの代わりにシングルクォーテーション''で囲んでもよい。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print("Python")
  print("Python")
   
   
14行目: 14行目:
<br>
<br>
次に、以下のように入力すると、Python PHPと出力される。<br>
次に、以下のように入力すると、Python PHPと出力される。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print("Python", "PHP")
  print("Python", "PHP")
   
   
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=",")
   
   
36行目: 36行目:
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")
   
   
45行目: 45行目:
次に、以下のように、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")
55行目: 55行目:
また、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")
   
   
65行目: 65行目:
== 変数の宣言と使用 ==
== 変数の宣言と使用 ==
===== 基本 =====
===== 基本 =====
  <source lang="python">
  <syntaxhighlight lang="python">
  msg = "こんにちは、Python"
  msg = "こんにちは、Python"
  num = 100
  num = 100
77行目: 77行目:
<br>
<br>
次にように、文字列リテラルを代入した変数msgに、数値リテラルを代入する場合、変数msgは自動的に整数型と認識される。<br>
次にように、文字列リテラルを代入した変数msgに、数値リテラルを代入する場合、変数msgは自動的に整数型と認識される。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  msg = "こんにちは、Python"
  msg = "こんにちは、Python"
  num = 100
  num = 100
89行目: 89行目:
Pythonでは、変数を宣言する時、データ型を自動的に認識するので型宣言は不要である。(これを動的型付けと呼ぶ)<br>
Pythonでは、変数を宣言する時、データ型を自動的に認識するので型宣言は不要である。(これを動的型付けと呼ぶ)<br>
ただし、型宣言があると理解しやすい時もあるので、以下のように、変数の型も併せて記述する方法もある。<br>
ただし、型宣言があると理解しやすい時もあるので、以下のように、変数の型も併せて記述する方法もある。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  msg: str = "こんにちは、Python"
  msg: str = "こんにちは、Python"
  num: int = 100
  num: int = 100
97行目: 97行目:
<br>
<br>
変数の型を知りたい場合、type関数を使用してデータ型を表示させることができる。<br>
変数の型を知りたい場合、type関数を使用してデータ型を表示させることができる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  msg = "こんにちは、Python"
  msg = "こんにちは、Python"
  num = 100
  num = 100
111行目: 111行目:
文字列型の変数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)
133行目: 133行目:
このように、文字列は順序を持つ型なのでシーケンス型のひとつになっている。<br>
このように、文字列は順序を持つ型なのでシーケンス型のひとつになっている。<br>
<br>
<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print('Hello')
  print('Hello')
  print("Hello")
  print("Hello")
153行目: 153行目:
===== 改行 =====
===== 改行 =====
改行する箇所に、\nを入れる。<br>
改行する箇所に、\nを入れる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  print("Hello, \nPython")
  print("Hello, \nPython")
   
   
163行目: 163行目:
複数行にわたる長い文章の表示は、トリプルクォート(""" """)で囲む。<br>
複数行にわたる長い文章の表示は、トリプルクォート(""" """)で囲む。<br>
トリプルクォートで囲んだ部分は、改行も自由にできる。<br>
トリプルクォートで囲んだ部分は、改行も自由にできる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  strhoge = """Hello,  
  strhoge = """Hello,  
               Python"""
               Python"""
175行目: 175行目:
文字列が長くなって読みにくい場合は、丸括弧()の中で改行する文字列ごとにダブルクォーテーションで囲むか、<br>
文字列が長くなって読みにくい場合は、丸括弧()の中で改行する文字列ごとにダブルクォーテーションで囲むか、<br>
改行する場所でバックスラッシュ\を入れることでコードを途中で改行して見やすくすることができる。<br>
改行する場所でバックスラッシュ\を入れることでコードを途中で改行して見やすくすることができる。<br>
  <source lang="python">
  <syntaxhighlight lang="python">
  str1 = ("Hello, "
  str1 = ("Hello, "
         "Python")
         "Python")
202行目: 202行目:
文字列型の変数の後ろに[]を使用して、その中にインデックスを指定する。
文字列型の変数の後ろに[]を使用して、その中にインデックスを指定する。
例えば、以下のように、変数wordの後ろに[]をつけて抽出する文字列のインデックスを指定する。
例えば、以下のように、変数wordの後ろに[]をつけて抽出する文字列のインデックスを指定する。
  <source lang="python">
  <syntaxhighlight lang="python">
  word = "python"
  word = "python"
  print(word[0])
  print(word[0])
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])
249行目: 249行目:
== 文字列の検索・置換・結合 ==
== 文字列の検索・置換・結合 ==
===== 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))
259行目: 259行目:
===== 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
275行目: 275行目:
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())
284行目: 284行目:
<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("."))
293行目: 293行目:
<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"))
303行目: 303行目:
===== 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"
315行目: 315行目:
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"))
324行目: 324行目:
<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"))
334行目: 334行目:
===== 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"))
346行目: 346行目:
===== 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"))
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())

案内メニュー