{"id":24652,"date":"2021-10-25T09:12:10","date_gmt":"2021-10-25T03:42:10","guid":{"rendered":"https:\/\/python-programs.com\/?p=24652"},"modified":"2021-11-05T19:14:51","modified_gmt":"2021-11-05T13:44:51","slug":"python-string-encode-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-encode-method-with-examples\/","title":{"rendered":"Python String encode() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Pass Statement with Examples<\/a> The encode() method encodes the string using the encoding specified. When no encoding is specified, UTF-8 is used.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:<\/strong><\/p>\n encoding:<\/strong> This is Optional. A String indicating the encoding to be used. UTF-8 is the default.<\/p>\n errors:<\/strong> This is Optional. The error method is specified as a String. The following are legal values:<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n These examples demonstrate the use of ascii encoding and a character that cannot be encoded, with various errors:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n String Encoding:<\/strong><\/p>\n Strings have been stored as Unicode since Python 3.0, which means that each character in the string is represented by a code point. As a result, each string is simply a series of Unicode code points.<\/p>\n The sequence of code points is converted into a set of bytes for efficient storage of these strings. Encoding is the name given to this process.<\/p>\n There are numerous encodings available, each of which treats a string differently. Popular encodings include utf-8, ascii, and others.<\/p>\n Using the string encode() method, you can convert unicode strings to any Python encoding. Python’s default encoding is utf-8.<\/p>\n Go through our tutorial and learn about various Python String Method Examples<\/a> and learn how to apply the knowledge while dealing with strings.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program for Pass Statement with Examples String encode() Method in Python: The encode() method encodes the string using the encoding specified. When no encoding is specified, UTF-8 is used. Syntax: string.encode(encoding=encoding, errors=errors) Parameter Values: encoding: This is Optional. A String indicating the encoding to be used. UTF-8 …<\/p>\n
\nString encode() Method in Python:<\/strong><\/p>\nstring.encode(encoding=encoding, errors=errors)<\/pre>\n
\n
Given string = \"Welcome to Pyth\u00f6n-pr\u00f6grams\"<\/pre>\n
b'Welcome to Pyth\\\\xf6n-pr\\\\xf6grams'\r\nb'Welcome to Pythn-prgrams'\r\nb'Welcome to Pyth\\\\N{LATIN SMALL LETTER O WITH DIAERESIS}n-pr\\\\N{LATIN SMALL LETTER O WITH DIAERESIS}grams'\r\nb'Welcome to Pyth?n-pr?grams'\r\nb'Welcome to Pythön-prögrams'<\/pre>\n
Given string = \"Hello this is vikr\u00e5m\"<\/pre>\n
b'Hello this is vikr\\\\xe5m'\r\nb'Hello this is vikrm'\r\nb'Hello this is vikr\\\\N{LATIN SMALL LETTER A WITH RING ABOVE}m'\r\nb'Hello this is vikr?m'\r\nb'Hello this is vikråm'<\/pre>\n
String encode() Method Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the string as static input() and store it in a variable.\r\ngvn_str = \"Welcome to Pyth\u00f6n-pr\u00f6grams\"\r\n# Give the encoding value as ascii and errors as \"backslashreplace\" for the\r\n# given string using the encode function and print it.\r\n# Here\u00a0\"backslashreplace\" employs a backslash in place of the unencoded character.\r\nprint(gvn_str.encode(encoding=\"ascii\", errors=\"backslashreplace\"))\r\n# Give the encoding value as ascii and errors as \"ignore\" for the given\r\n# string using the encode function and print it.\r\n# Here \"ignore\" does not encode characters that cannot be encoded.\r\nprint(gvn_str.encode(encoding=\"ascii\", errors=\"ignore\"))\r\n# Give the encoding value as ascii and errors as \"namereplace\" for the given\r\n# string using the encode function and print it.\r\n# Here \"namereplace\" replaces the character with text that describes the character.\r\nprint(gvn_str.encode(encoding=\"ascii\", errors=\"namereplace\"))\r\n# Give the encoding value as ascii and errors as \"replace\" for the given\r\n# string using the encode function and print it.\r\n# Here \"replace\" inserts a question mark in place of the character.\r\nprint(gvn_str.encode(encoding=\"ascii\", errors=\"replace\"))\r\n# Give the encoding value as ascii and errors as \"xmlcharrefreplace\" for the\r\n# given string using the encode function and print it.\r\n# Here \"xmlcharrefreplace\" is replacing\u00a0a character with an xml character.\r\nprint(gvn_str.encode(encoding=\"ascii\", errors=\"xmlcharrefreplace\"))\r\n<\/pre>\n
b'Welcome to Pyth\\\\xf6n-pr\\\\xf6grams'\r\nb'Welcome to Pythn-prgrams'\r\nb'Welcome to Pyth\\\\N{LATIN SMALL LETTER O WITH DIAERESIS}n-pr\\\\N{LATIN SMALL LETTER O WITH DIAERESIS}grams'\r\nb'Welcome to Pyth?n-pr?grams'\r\nb'Welcome to Pythön-prögrams'<\/pre>\n
Method #2: UTF-8 encoding to the given string (Static Input)<\/h3>\n
gvn_str = \"Hello this is vikr\u00e5m\"\r\nrslt = gvn_str.encode()\r\nprint(rslt)\r\n<\/pre>\n
b'Hello this is vikr\\xc3\\xa5m'<\/pre>\n
\n