Python str() Function | Examples

In Python, the “string” data type is used to save any single character or sequence of characters. The string is a primitive data type normally used in programming for data manipulation. Strings are represented using single or double quotation marks in Python. Everything written inside those quotation marks is considered a string value.

If you use any data type other than strings, Python allows you to change it to string using the “str()” function. Python’s str()” function is briefly demonstrated in this tutorial with multiple examples. This Python guide discusses the following topics:

So, let’s get started!

What is Python str() Function?

The inbuilt “str()” function of Python returned a new value as of the “string” data type. The original value will not be changed because most of the string methods in Python return the new value. This “str()” function takes different data types, such as “list”, “integer”, “float”, “dict”, etc., and converts them into a string. Let’s see the syntax of Python “str()” function:

Syntax:

str(object, encoding='utf-8', errors='strict')

Here, the “object” parameter represents the value that will be returned as a string. The “encoding” and “errors” parameters are optional, they are only used when the input data type is “bytes”or “byte array”.

Let’s begin with the first Python “str()” function example:

Example 1: Using “str()” to Convert Different Types Into String

In the example given below, different data types are represented as a string using the Python “str()” function. Python “str()” takes the object’s value inside the parenthesis and returns the string as an output. Let us see an example of code mentioned below: 

Code:

# Normal string representation
var_1 = str('itslinuxfoss')
print(var_1)
print(type(var_1))

# integer value string representation
value_number = str(140)
print(value_number)
print(type(value_number))

# float value string representation
float_number = str(9.2)
print(float_number)
print(type(float_number))

# numeric string value string representation
height_value = str('8ft Tall')
print(height_value)
print(type(height_value))

In the following code:

  • The “str()” function is used to convert the “four” different data types.
  • Firstly, any normal sequence of alphabetic characters, such as “itslinuxfoss” can be represented as a “string” by simply passing the value inside the parenthesis and being enclosed in the quotation marks.
  • In the second part, the integer value “140” is converted into a string with the help of the “str()” function.
  • Similarly, the float value “9.2” is also converted to a string using the same “str()” function.
  • The “numeric value with a string” can also be represented as a string. We just need to pass the value as an argument of the “str()” function, and the value must be enclosed by quotation marks.
  • The “type()” function is used after every “str()” function to verify the data type of the final output.
  • The final output will be in string data type.

Output:

The output shows the value of different data types in the “string” representation.

Example 2: Using “str()” to Convert a List Value Into String

The built-in “str()” function can also be used to represent a List value as a string. The following example code returns the string representation of list:

Code:

List_variable = [55.93, 'itslinuxfoss', 125]
string_return = str(List_variable)

print('Original Datatype is: ',type(List_variable))
print('After using str(): ',type(string_return))
print(string_return)

In the following code example:

  • The three elements inside the list value are initialized as a list in a list variable named “list_variable”.
  • The Python “str()” function takes the list variable as an argument and returns it into a string value.
  • The “type()” function prints the data type value of the original variable and data type after conversion of the variables.

Output:

The output shows that the “list” type is converted into the “string” data type.

Example 3: Using “str()” to Convert a Byte Object (Octal) to String

In Python, byte objects are sequences of bytes that allow one to manipulate binary data. The python “str()” function in the below example represent byte object as a string:

Code:

byte_object = b'\101\102\103\104\105'
final_string = str(byte_object, encoding='utf-8')
print(final_string)
print(type(final_string))

In the above code:

  • The “byte” object “octal” value is initialized and stored in the variable named “byte_object
  • The “str()” function takes two parameter values named as “byte_object” and “encoding”. The encoding parameter value will be utf-8 (encoding system for Unicode).
  • The “type()” function shows the data type of the input byte object after conversion.

Output:

The output shows the spring-type representation of the byte object.

Conclusion

In Python, the “str()” function returns any input data type as a string. Different data types, such as list, dict, etc., are converted into strings using this function. The “str()” function also converts the byte object into a string. The “list” containing different data types elements can also be transformed into a string using the “str()” function. The string conversion makes us able to handle textual data in Python. This write-up provided a complete guide on Python’s “str()” function with a list of examples.