Python – Create a string using str() built-in function

Create a string using str() Builtin Function

To create a string using str() builtin function, pass the string literal of value of another datatype to the function. The function returns a string object.

Example

In the following program, we create strings using str() builtin function, from different type of input value passed to str() function.

Python Program

#string literal with single quotes
x = str('apple')
print(x)

#string literal with double quotes
x = str("apple")
print(x)

#string from a number
x = str(1024)
print(x)

#string from a complex value
x = str(6 + 4j)
print(x)

#string from a list
x = str([4, 'apple'])
print(x)
Run Code Online

Output

apple
apple
1024
(6+4j)
[4, 'apple']

Summary

In this tutorial of Python Examples, we learned how to create string object using str() builtin function, with the help of well detailed examples.