{"id":24881,"date":"2021-11-10T09:36:57","date_gmt":"2021-11-10T04:06:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=24881"},"modified":"2021-11-10T09:36:57","modified_gmt":"2021-11-10T04:06:57","slug":"python-str-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-str-function-with-examples\/","title":{"rendered":"Python str() Function with Examples"},"content":{"rendered":"
str() Function in Python:<\/strong><\/p>\n The str() function returns a string from the specified value.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n object:<\/strong> It may be any object. The object to be converted into a string is specified.<\/p>\n encoding:<\/strong> The object’s encoding. UTF-8 is the default.<\/p>\n errors:<\/strong> Specifies what should be done if the decoding fails.<\/p>\n Errors are classified into six types:<\/p>\n strict –<\/strong> the default response, which throws a UnicodeDecodeError exception if it fails.<\/p>\n ignore<\/strong> – removes any unencodable Unicode from the output.<\/p>\n replace<\/strong> – converts an unencodable Unicode character to a question mark.<\/p>\n xmlcharrefreplace<\/strong> – inserts an XML character reference rather than an unencodable Unicode character.<\/p>\n backslashreplace<\/strong> – replaces unencodable Unicode with an \/uNNNN espace sequence.<\/p>\n namereplace<\/strong> – replaces unencodable Unicode with an N… escape sequence.<\/p>\n Return Value:<\/strong><\/p>\n The str() method returns a string, which is an informal or nicely printable representation of the object passed in.<\/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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If both the encoding and errors parameters are specified, the first parameter, object, must be a bytes-like-object (bytes or bytearray).<\/p>\n If the object is bytes or bytearray, str() calls bytes internally.<\/p>\n bytes.decode(encoding, errors)<\/strong><\/p>\n Otherwise, it loads the bytes object into the buffer before invoking the decode() method.<\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n ASCII cannot decode the character ‘\u00f6’ in this case. As a result, it should throw an error. We have, however, set the errors to ‘ignore’. As a result, Python ignores the character that cannot be decoded by str ().<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n str() Function in Python: The str() function returns a string from the specified value. Syntax: str(object, encoding=encoding, errors=errors) Parameters object: It may be any object. The object to be converted into a string is specified. encoding: The object’s encoding. UTF-8 is the default. errors: Specifies what should be done if the decoding fails. Errors are …<\/p>\nstr(object, encoding=encoding, errors=errors)<\/pre>\n
Given Number = 25<\/pre>\n
The given number after converting it into a string = 25\r\n<class 'str'><\/pre>\n
Given Number = 80<\/pre>\n
The given number after converting it into a string = 80\r\n<class 'str'><\/pre>\n
str() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 25\r\n# Pass the given number as an argument to the str() function that converts\r\n# the given number into a string. (integer to string)\r\n# Store it in another variable.\r\nstr_numb = str(gvn_numb)\r\n# Print the given number after converting it into a string.\r\nprint(\"The given number after converting it into a string = \", str_numb)\r\n# Print the type of the above result.(string number)\r\nprint(type(str_numb))\r\n<\/pre>\n
The given number after converting it into a string = 25\r\n<class 'str'><\/pre>\n
str() for bytes<\/h5>\n
# str() for bytes\r\nk = bytes('Pr\u00f6grams', encoding='utf-8')\r\nprint(str(k, encoding='ascii', errors='ignore'))\r\n<\/pre>\n
Prgrams<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Pass the given number as an argument to the str() function that converts\r\n# the given number into a string. (integer to string)\r\n# Store it in another variable.\r\nstr_numb = str(gvn_numb)\r\n# Print the given number after converting it into a string.\r\nprint(\"The given number after converting it into a string = \", str_numb)\r\n# Print the type of the above result.(string number)\r\nprint(type(str_numb))\r\n<\/pre>\n
Enter some random number = 70\r\nThe given number after converting it into a string = 70\r\n<class 'str'><\/pre>\n","protected":false},"excerpt":{"rendered":"