Create a String in Python: Single/Double/Triple Quotes, str()
In Python, strings (str) can be created by enclosing text in single quotes ', double quotes ", and triple quotes (''', """). You can also convert objects of other types into strings using str().
Single quotes: '
To create a string, enclose the text in single quotes '.
s = 'abc'
print(s)
# abc
print(type(s))
# <class 'str'>
Double quotes: "
Alternatively, you can enclose the text in double quotes " to create a string.
s = "abc"
print(s)
# abc
print(type(s))
# <class 'str'>
Difference between single quotes and double quotes
Both values are equal
Regardless of whether you use single quotes ' or double quotes ", the resulting strings are equal.
s_sq = 'abc'
s_dq = "abc"
print(s_sq == s_dq)
# True
Quotes in strings are handled differently
In a string enclosed with single quotes ', you can use double quotes " directly. However, single quotes ' need to be escaped with a backslash, like this: \'. Writing \" for double quotes within the single-quoted string is also permissible, but unnecessary.
s_sq = 'a\'b"c'
print(s_sq)
# a'b"c
s_sq = 'a\'b\"c'
print(s_sq)
# a'b"c
In a string enclosed with double quotes ", you can use single quotes ' directly. However, double quotes " need to be escaped with a backslash, like this: \". Writing \' for single quotes within the double-quoted string is also permissible, but unnecessary.
s_dq = "a'b\"c"
print(s_dq)
# a'b"c
s_dq = "a\'b\"c"
print(s_dq)
# a'b"c
Since the difference is only in notation, the resulting values are equal in both cases.
s_sq = 'a\'b"c'
s_dq = "a'b\"c"
print(s_sq == s_dq)
# True
Triple quotes: ''', """
Triple quotes, either three single quotes ''' or three double quotes """, can also be used to create a string.
Multiple lines
An error will occur if you insert a newline directly into a string enclosed by single or double quotes. To insert a newline, you need to use \n.
# s = 'abc
# xyz'
# SyntaxError: EOL while scanning string literal
s = 'abc\nxyz'
print(s)
# abc
# xyz
Within a string enclosed in triple quotes, line breaks can be directly included without any additional escaping.
s_tq = '''abc
xyz'''
print(s_tq)
# abc
# xyz
print(type(s_tq))
# <class 'str'>
Of course, a triple-quoted string does not always have to include line breaks.
s_tq = '''abc'''
print(s_tq)
# abc
Single and double quotes
You can use double quotes " in three single quotes ''' and single quotes ' in three double quotes """. Additionally, you can use escaped single \' or double quotes \" within both types of triple quotes. In all cases, the resulting strings are equal.
s_tq_sq = '''\'abc\'
"xyz"'''
print(s_tq_sq)
# 'abc'
# "xyz"
s_tq_dq = """'abc'
\"xyz\""""
print(s_tq_dq)
# 'abc'
# "xyz"
print(s_tq_sq == s_tq_dq)
# True
Indentation
If spaces are added at the beginning of a line to match the indentation, the resulting string will include these spaces.
s_tq = '''abc
xyz'''
print(s_tq)
# abc
# xyz
An alternative way to write multiline strings is by using line breaks and parentheses.
s_multi = ('abc\n'
'xyz')
print(s_multi)
# abc
# xyz
See the following article for details.
Convert other types to strings: str()
You can use str() to convert objects of other types to strings (str).
str() returns the result of the __str__() method of the target object. If its type has no __str__() method defined, it returns the result of repr().
Convert numbers to strings
Integers (int) and floating point numbers (float) can be converted to strings (str) using str().
i = 100
s_i = str(i)
print(s_i)
# 100
print(type(s_i))
# <class 'str'>
f = 0.123
s_f = str(f)
print(s_f)
# 0.123
print(type(s_f))
# <class 'str'>
For example, even if int values are in hexadecimal or float values are in scientific notation, str() converts them to standard decimal strings.
i = 0xFF
print(i)
# 255
s_i = str(i)
print(s_i)
# 255
f = 1.23e+10
print(f)
# 12300000000.0
s_f = str(f)
print(s_f)
# 12300000000.0
To convert a value to a string in a specific format, use the built-in format() function.
s_i_format = format(i, '#X')
print(s_i_format)
# 0XFF
s_f_format = format(f, '.2e')
print(s_f_format)
# 1.23e+10
To convert a string of numbers to numeric values, refer to the following article.
Convert lists and dictionaries to strings
You can also convert lists (list) and dictionaries (dict) to strings (str) using str().
l = [0, 1, 2]
s_l = str(l)
print(s_l)
# [0, 1, 2]
print(type(s_l))
# <class 'str'>
d = {'a': 1,
'b': 2,
'c': 3}
s_d = str(d)
print(s_d)
# {'a': 1, 'b': 2, 'c': 3}
print(type(s_d))
To convert a list or dictionary to a well-formatted string, you can use the pformat() function from the pprint module in the standard library.
import pprint
dl = {'a': 1, 'b': 2, 'c': [100, 200, 300]}
s_dl = str(dl)
print(s_dl)
# {'a': 1, 'b': 2, 'c': [100, 200, 300]}
p_dl = pprint.pformat(dl, width=10)
print(p_dl)
# {'a': 1,
# 'b': 2,
# 'c': [100,
# 200,
# 300]}
print(type(p_dl))
# <class 'str'>
For more information on the pprint module, refer to the following article.