Python-string-encode

提供:Dev Guides
移動先:案内検索

Python String encode()メソッド

説明

Python文字列メソッド* encode()*は、文字列のエンコードされたバージョンを返します。 デフォルトのエンコーディングは、現在のデフォルトの文字列エンコーディングです。 エラーは、異なるエラー処理スキームを設定するために与えられます。

構文

str.encode(encoding='UTF-8',errors='strict')

パラメーター

  • encoding -これは使用されるエンコーディングです。 すべてのエンコーディングスキームのリストについては、http://docs.python.org/library/codecsl#standard-encodings [Standard Encodings。]をご覧ください。
  • エラー-これは、異なるエラー処理スキームを設定するために指定できます。 エラーのデフォルトは「strict」です。つまり、エンコードエラーはUnicodeErrorを発生させます。 他の可能な値は、「ignore」、「replace」、「xmlcharrefreplace」、「backslashreplace」、およびcodecs.register_error()を介して登録されたその他の名前です。

戻り値

デコードされた文字列。

#!/usr/bin/python

str = "this is string example....wow!!!";
print "Encoded String: " + str.encode('base64','strict')

結果

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=