Home >Backend Development >Python Tutorial >Python program creates string object
In Python, we can create string objects using Python’s built-in function str() and assigning a sequence of characters to a variable. Character sequences are enclosed in single or double quotes. We can also create multi-line string objects using triple quotes. In this article, we will look at the various ways to create string objects in Python.
We can create a string object by simply assigning a sequence of characters enclosed in single quotes to a variable.
my_string = 'Hello World!' print(my_string)
Hello World!
We can create a string object by placing a sequence of characters in double quotes and assigning it to a variable.
my_string = "Hello World!" print(my_string)
Hello World!
In Python, we can create a multiline string object by enclosing the multiline string in triple quotes or double triple quotes.
my_string = '''This is a multiline String''' print(my_string)
This is a multiline String
The str() function can be used to convert any data type object to a string object. In the following example, we use str() function to convert integer data type to string and assign it to a variable.
my_number = 123 my_string = str(my_number) print(my_string)
123
In Python, we can use single quotes, double quotes, triple quotes to double triple quotes to create string objects. Triple quotes are used to create multiline string objects. Python also provides the str() function to convert any data type object to a string object. In this article, we learned about all the ways to create string objects in Python.
The above is the detailed content of Python program creates string object. For more information, please follow other related articles on the PHP Chinese website!