If my input text is
a
b
c
d
e
f
g
and I want my output text to be: [with the double quotes]
"a b c d e f g"
Where do I go after this step:
" ".join[[a.strip[] for a in b.split["\n"] if a]]
asked Jul 22, 2016 at 21:46
1
You have successfully constructed a string without the quotes. So you need to add the double quotes. There are a few different ways to do this in Python:
>>> my_str = " ".join[[a.strip[] for a in b.split["\n"] if a]]
>>> print '"' + my_str + '"' # Use single quotes to surround the double quotes
"a b c d e f g"
>>> print "\"" + my_str + "\"" # Escape the double quotes
"a b c d e f g"
>>> print '"%s"' % my_str # Use old-style string formatting
"a b c d e f g"
>>> print '"{}"'.format[my_str] # Use the newer format method
"a b c d e f g"
Or in Python 3.6+:
>>> print[f'"{my_str}"'] # Use an f-string
"a b c d e f g"
Any of these options are valid and idiomatic Python. I might go with the first option myself, or the last in Python 3, simply because they're the shortest and clearest.
Quoth
32 silver badges4 bronze badges
answered Jul 22, 2016 at 21:58
JamesJames
2,7741 gold badge13 silver badges24 bronze badges
'"%s"' % " ".join[[a.strip[] for a in s.split["\n"] if a]]
answered Jul 22, 2016 at 21:51
CentAuCentAu
9,91414 gold badges55 silver badges79 bronze badges
Add quotes to a string in Python #
To add quotes to a string in Python:
- Alternate between single and double quotes.
- For example, to add double quotes to a string, wrap the string in single quotes.
- To add single quotes to a string, wrap the string in double quotes.
Copied!
# 👇️ alternating single and double quotes result_1 = '"apple"' # 👇️ using a formatted string literal my_str = 'apple' result_2 = f'"{my_str}"' # 👇️ escaping double quotes with a backslash result_3 = "\"apple\""
The first example in the code snippet alternates between single and double quotes.
Copied!
result_1 = '"apple"'
If a string is wrapped in single quotes, we can use double quotes in the string without any issues.
However, if we try to use single quotes in a string that was wrapped in single quotes, we end up terminating the string prematurely.
If you need to add single quotes to a string, wrap the string in double quotes.
Copied!
result_1 = "one 'two' three"
In some rare cases your string might contain both single and double quotes. To get around this, use a triple-quoted string.
Copied!
result_1 = """ "one" two 'three' """
Triple-quotes strings are very similar to basic strings that we declare using single or double quotes.
But they also enable us to:
- use single and double quotes in the same string without escaping
- define a multi-line string without adding newline characters
Copied!
example = ''' It's Alice "hello" ''' # # It's Alice # "hello" # print[example]
The string in the example above uses both single and double quotes and doesn't have to escape anything.
End of lines are automatically included in triple-quoted strings, so we don't have to add a newline character at the end.
An alternative is to use a formatted string literal.
Copied!
my_str = 'one' result_2 = f'"{my_str}" "two"' print[result_2] # 👉️ '"one" "two"'
Notice that we still have to alternate between single and double quotes.
Formatted string literals [f-strings] let us include expressions inside of a string by prefixing the string with f
.
Copied!
my_str = 'is subscribed:' my_bool = True result = f'{my_str} "{my_bool}"' print[result] # 👉️ 'is subscribed: "True"'
Make sure to wrap expressions in curly braces - {expression}
.
You can also use a backslash \
to escape quotes.
Copied!
result_3 = "\"one\" \"two\"" print[result_3] # 👉️ '"one" "two"'
In most cases, it is preferable [and more readable] to alternate between single and double quotes, but escaping quotes can also be useful [e.g. in rare cases in a JSON string].