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 classified into six types:
strict – the default response, which throws a UnicodeDecodeError exception if it fails.
ignore – removes any unencodable Unicode from the output.
replace – converts an unencodable Unicode character to a question mark.
xmlcharrefreplace – inserts an XML character reference rather than an unencodable Unicode character.
backslashreplace – replaces unencodable Unicode with an /uNNNN espace sequence.
namereplace – replaces unencodable Unicode with an N… escape sequence.
Return Value:
The str() method returns a string, which is an informal or nicely printable representation of the object passed in.
Examples:
Example1:
Input:
Given Number = 25
Output:
The given number after converting it into a string = 25 <class 'str'>
Example2:
Input:
Given Number = 80
Output:
The given number after converting it into a string = 80 <class 'str'>
str() Function with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Pass the given number as an argument to the str() function that converts the given number into a string. (integer to string)
- Store it in another variable.
- Print the given number after converting it into a string.
- Print the type of the above result (string number).
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numb = 25 # Pass the given number as an argument to the str() function that converts # the given number into a string. (integer to string) # Store it in another variable. str_numb = str(gvn_numb) # Print the given number after converting it into a string. print("The given number after converting it into a string = ", str_numb) # Print the type of the above result.(string number) print(type(str_numb))
Output:
The given number after converting it into a string = 25 <class 'str'>
str() for bytes
If both the encoding and errors parameters are specified, the first parameter, object, must be a bytes-like-object (bytes or bytearray).
If the object is bytes or bytearray, str() calls bytes internally.
bytes.decode(encoding, errors)
Otherwise, it loads the bytes object into the buffer before invoking the decode() method.
# str() for bytes k = bytes('Prögrams', encoding='utf-8') print(str(k, encoding='ascii', errors='ignore'))
Output:
Prgrams
Explanation:
ASCII cannot decode the character ‘ö’ 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 ().
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Pass the given number as an argument to the str() function that converts the given number into a string. (integer to string)
- Store it in another variable.
- Print the given number after converting it into a string.
- Print the type of the above result (string number).
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function and store it in a variable. gvn_numb = int(input("Enter some random number = ")) # Pass the given number as an argument to the str() function that converts # the given number into a string. (integer to string) # Store it in another variable. str_numb = str(gvn_numb) # Print the given number after converting it into a string. print("The given number after converting it into a string = ", str_numb) # Print the type of the above result.(string number) print(type(str_numb))
Output:
Enter some random number = 70 The given number after converting it into a string = 70 <class 'str'>