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

Python program creates string object

PHPz
Release: 2023-09-05 16:37:05
forward
1642 people have browsed it

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)
Copy after login

Output

Hello World!
Copy after login
Copy after login

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)
Copy after login

Output

Hello World!
Copy after login
Copy after login

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)
Copy after login

Output

This is a multiline
String
Copy after login

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)
Copy after login

Output

123
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template