How to Create a String in Python
To create a string in Python, you can use single quotes (’ ‘), double quotes (" “), or triple quotes (”"" “”").
The following example shows how to create a string in Python using three different methods.
Using Single Quotes
Let’s see how to create a string in Python using ’’ single quotes:
# Create string using single quotes
string1 = 'Hello, world!'
# Show string
print(string1)
Output: 👇️
Hello, world!
The output shows the string created using single quotes.
Using Double Quotes
You can use "" double quotes to create a string in Python:
# Create string using double quotes
string2 = "Hello, world!"
# Show string
print(string2)
Output: 👇️
Hello, world!
The output shows the string created using double quotes.
Using Triple Quotes
We can use """""" triple quotes in Python to create a multi-line string:
# Create string using triple quotes
string3 = '''This is a multi-line string.
It can span multiple lines.
'''
# Show string
print(string3)
Output: 👇️
This is a multi-line string.
It can span multiple lines.
In the above code, triple quotes are used to create a multi-line string.
Conclusion
We can use single quotes, double quotes, or triple quotes to create a string in Python.