{"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

str(object, encoding=encoding, errors=errors)<\/pre>\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

Given Number = 25<\/pre>\n

Output:<\/strong><\/p>\n

The given number after converting it into a string =  25\r\n<class 'str'><\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Number = 80<\/pre>\n

Output:<\/strong><\/p>\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