Creating Strings in Python Using the str() Built-in Function
Create a string using str() Builtin Function
To create a string using str()
builtin function, pass required string literal or a value of another datatype, as argument to the function. The function returns a string object created from the given value.
Example
In the following program, we create strings using str() builtin function, from different types of input values, using str() function.
Python Program
# Create string from string literal with single quotes
x = str('apple')
print(x)
# Create string from string literal with double quotes
x = str("apple")
print(x)
# Create string from a number
x = str(1024)
print(x)
# Create string from a complex value
x = str(6 + 4j)
print(x)
# Create string from a list
x = str([4, 'apple'])
print(x)
Output
apple
apple
1024
(6+4j)
[4, 'apple']
Video Tutorial
In the following video tutorial, you shall learn how to create a string in Python with well detailed explanation and examples.
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.