Home >Backend Development >Python Tutorial >Python program creates string object

Python program creates string object

PHPz
PHPzforward
2023-09-05 16:37:051596browse

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.

Example 1: Create a string object using single quotes

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)

Output

Hello World!

Example 2: Using double quotes to create a string object

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)

Output

Hello World!

Example 3: Using triple quotes to create a multi-line string object

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)

Output

This is a multiline
String

Example 4: Create a string object using the str() function

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)

Output

123

in conclusion

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete