logo

Python のアルファベット

このチュートリアルでは、アルファベット リストの作成に使用できるさまざまな Python 関数を学びます。これらの機能は、プログラミングコンテストや面接の問題の準備に非常に役立ちます。 Python 文字列モジュールを使用すると、ASCII アルファベットの小文字と大文字すべてのリストを作成する方法がわかります。 Python の組み込み ord() メソッドと chr() メソッドに依存する基本的な実装についても説明します。

String モジュールを使用して Python のアルファベットのリストを作成する

Python 文字列モジュールを使用することは、アルファベットのすべての文字のリストを作成する最も迅速かつ自然な方法です。 Python 文字列モジュールはデフォルトの Python ライブラリのメンバーであるため、インストールする必要があるものは何もありません。 string.ascii 文字、string.ascii 小文字、および string.ascii 大文字のインスタンスを使用すると、すべてのアルファベット文字のリストを簡単に取得できます。

string モジュールのこれらのインスタンスは、名前で示されている小文字と大文字のアルファベット、および適切な小文字と大文字のアルファベットを返します。値は定数であり、ロケールに依存しません。したがって、どのロケールを指定しても、常に同じ結果が得られます。

string モジュールを使用して、Python で小文字のアルファベットをロードする方法を見てみましょう。

コード

 # Python program to print a list of alphabets # Importing the string module import string # Printing a list of lowercase alphabets lower = list(string.ascii_lowercase) print(lower) # Printing a list of uppercase alphabets upper = list(string.ascii_uppercase) print(upper) # Printing a list of both upper and lowercase alphabets alphabets = list(string.ascii_letters) print(alphabets) 

出力:

 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] 

Python chr 関数と ord 関数の使用

このパートでは、組み込みの chr 関数と ord 関数を使用してアルファベット リストを作成する方法を説明します。整数値は、Python chr 関数を使用して、対応する Unicode 値に変換されます。 ord 関数は、Unicode 値を同等の整数に変換して同じことを行います。

For a ループを使用してアルファベットのリストを作成する

小文字の文字のリストを作成するには、chr() メソッドを使用して 97 から 122 までの整数値をループします。 a から z までの小文字を表すには、97 ~ 122 の範囲の整数が使用されます。作成する空のリストに各文字を追加します。これがどのように表示されるかを確認してください。

コード

int Java へのロング
 # Python program to generate a list of alphabets using the chr and ord functions list_ = [] for i in range(97, 123): list_.append(chr(i)) print(list_) 

出力:

 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] 

97 (または 122) が何を表しているかを思い出すのは難しい場合があります。これにより、 ord() メソッドを使用してアルファベット「g」の整数値を取得した後、他の 26 文字を循環することができます。これを見てみましょう。

コード

 # Python program to show how to use the ord function to retrieve the integral value of any alphabet list_ = [] # Getting the integral value of the letter 'j' start_from = ord('g') for i in range(20): list_.append(chr(start_from + i)) print(list_) 

出力:

 ['g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] 

Python リスト内包表記を使用してアルファベットのリストを作成する

式は、指定された反復可能オブジェクト内のすべての項目に対して評価されることはすでにわかっています。これを達成するには、97 から 122 までの Python 範囲オブジェクトを反復処理して、アルファベットの Python リストを作成します。今回は、リスト内包表記を使用してこれを行います。

コード

 # Python program to generate a list of alphabets using the Python list comprehension and the chr() function # Initializing the list comprehension listt = [chr(v) for v in range(97, 123)] # Printing the list print(listt) 

出力:

 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] 

for ループは特に複雑ではありませんでしたが、これを Python のリスト内包表記にすることではるかに単純になりました。以下に示すように、追加の動的バージョンを Python リスト内包表記に変えることもできます。

空のJava

コード

 # Python program to generate a list of alphabets using the Python list comprehension and the ord() function # Initializing the list comprehension listt = [chr(v) for v in range(ord('a'), ord('a') + 26)] # Printing the list print(listt) 

出力:

 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] 

次のセクションでは、map() メソッドを使用してアルファベットの Python リストを作成する方法を説明します。

Map 関数を使用してアルファベットのリストを作成する

この部分では、map() メソッドを使用してアルファベットのリストを生成します。イテラブル内の各項目は、map 関数に指定された関数に渡されます。その結果、Python chr 関数を、アルファベットを含む範囲オブジェクトのすべての項目にマップできます。このメソッドは、反復可能な項目ごとにどのような操作が実行されるかを明確にすることで読みやすさを向上させます。

このコードの外観を調べてみましょう。

コード

 # Python program to generate a list of alphabets using the Python map and the ord() function # Initializing the map function listt = list(map(chr, range(97, 123))) # Printing the list print(listt) 

出力:

 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] 

ここでは、インタプリタが 97 から 123 までの range() オブジェクトのすべての項目にマップする chr 関数を、map() メソッドに与えます。 map() メソッドはマップ オブジェクトを提供するため、これをリストに変更するには list() メソッドを使用する必要があります。

Python 文字列 isalpha()

指定された文字列のすべての文字がアルファベットの場合、isalpha() 関数は True を返します。そうでない場合は、False が返されます。

10の6乗

Python isalpha() 関数の構文は次のとおりです。

 string.isalpha() 

isalpha() のパラメータ:

isalpha() 関数はパラメータを取りません。

isalpha() からの戻り値

isalpha() は次の結果を生成します。

  • 指定された文字列に英字のみが含まれる場合は True (文字列には小文字と大文字を含めることができます)。
  • 文字列のいずれかの文字がアルファベットではない場合は False。

例1

isalpha() の動作を見てみましょう

コード

マイクロリシックカーネル
 # Python program to show how the isalpha() function works # Giving a normal string with all the characters as alphabets website = 'Javatpoint' print(f'All the characters of {website} are alphabets: ', website.isalpha()) # Giving the string that contains whitespace name = 'Peter Parker' print(f'All the characters of {name} are alphabets: ', name.isalpha()) # Giving a string that contains the number name = 'Peter2' print(f'All the characters of {name} are alphabets: ', name.isalpha()) 

出力:

 All the characters of Javatpoint are alphabets: True All the characters of Peter Parker are alphabets: False All the characters of Peter2 are alphabets: False 

例 2

isalpha() 関数を if-else 節とともに使用する。

コード

 # Python program to show how the isalpha() function works with if-else conditions # Initializing the strings string1 = 'PeterParker' string2 = 'Peter Parker' # Using the if else condition statements # Giving the first string if string1.isalpha() == True: print('All the characters of the given string are alphabet') else: print('All the characters of the given string are not alphabet') # Giving the second string if string2.isalpha() == True: print('All the characters of the given string are alphabet') else: print('All the characters of the given string are not alphabet') 

出力:

 All the characters of the given string are alphabet All the characters of the given string are not alphabet