Can we perform the same operations on multi-line strings that we perform on other strings?
Answer
Yes, operations like concatenation can be performed to combine multi-line strings with single-line strings because strings created using the two techniques are both actually of the same type.
Representing String Literals in Python
In Python, a variety of techniques can be used to represent string literals in code. Each technique offers particular advantages, but all of the techniques create the same type, namely a string. Since they all create the same type of string object, concatenation can be used to combine two strings, regardless of whether they were created using different techniques.
Technique
Reasons for Using It
Double quote delimiter
Can include single quotes in the string without using an escape sequence
Single quote delimiter
Can include double quotes in the string without using an escape sequence
Raw string
Can include literal escape sequences in the string
Triple double quote delimiter
Can include multiple lines in the string without using an escape sequence
Triple single quote delimiter
Can include multiple lines in the string without using an escape sequence
Credit for this post goes to @appylpye. We hope this helps.
Here is a concatenation multi-line example where two long multi line strings from a poem are added together.
first_Multi_Line_String = """Had I the heaven's embroidered cloths,
Enwrought with golden and silver light,
The blue and the dim and the dark cloths
Of night and light and the half-light;
I would spread the cloths under your feet"""
second_Multi_Line_String = """ But I, being poor, have only my dreams;
I have spread my dreams under your feet;
Tread softly because you tread on my dreams."""
print(first_Multi_Line_String + second_Multi_Line_String)
##RESULT:
Had I the heaven’s embroidered cloths,
Enwrought with golden and silver light,
The blue and the dim and the dark cloths
Of night and light and the half-light;
I would spread the cloths under your feet
But I, being poor, have only my dreams;
I have spread my dreams under your feet;
Tread softly because you tread on my dreams.
When printing the three variable out, the previous output incurred an EOL error except the last one.
Hello, I have a question about quotation usage. Please see the above example. So does that mean only triple quotation marks can be used for multi-lines comments?
When concatenating an integer with strings (whether single or multi-line), we need to use the str() function on the integer. str() returns the string version of the object that is passed in.
Example
year = 2020
print("The year is " + year) # throws an error
print("The year is " + str(year)) # prints The year is 2020
Hey I am trying to just add a int value to the multi-line string variable to be placed on the same line but in the interpreter it places it below the multi-line string.
# Assign the string here
to_you = """
Stranger, if you passing meet me and
desire to speak to me,
why
should you not speak
to me?
Andy why should I not speak to you?
"""
print(to_you)
value = 6
print(to_you + str(value))
printed output on interpreter
Stranger, if you passing meet me and
desire to speak to me,
why
should you not speak
to me?
Andy why should I not speak to you?
Stranger, if you passing meet me and
desire to speak to me,
why
should you not speak
to me?
Andy why should I not speak to you?
6
You can also insert a variable containing an Integer value ( placed in brackets {} ) into your string literal by using String Interpolation, ['your string goes here with the variable value in brackets {}'].format([variableName]) , which is in a later lesson.
Below is an example:
value = 13
to_you = """Stranger, if you passing meet me and desire to speak to me, why
should you {} not speak to me?
And why should I not speak to you?""".format(value)
print(to_you)
Why does the second string starts in a newline ? It should not.
first_Multi_Line_String = """Had I the heaven's embroidered cloths,
Enwrought with golden and silver light,
The blue and the dim and the dark cloths
Of night and light and the half-light;
I would spread the cloths under your feet"""
second_Multi_Line_String = """ But I, being poor, have only my dreams;
I have spread my dreams under your feet;
Tread softly because you tread on my dreams."""
print(first_Multi_Line_String + second_Multi_Line_String)
By ‘second string’ are you referring to the ‘second_Multi_Line_String’ string variable in the Codebyte? If you are, then running the code shows that the ‘second_Multi_Line_String’ variable does not start on a newline. Three lines up from the bottom line you can see the part of the string that reads: ‘But I, being poor, have only my dreams;’ is not on a line of its own.
You could achieve this by incorporating the escape sequence (\n) before the second multiline string as follows:
first_Multi_Line_String = """Had I the heaven's embroidered cloths,
Enwrought with golden and silver light,
The blue and the dim and the dark cloths
Of night and light and the half-light;
I would spread the cloths under your feet"""
second_Multi_Line_String = """\nBut I, being poor, have only my dreams;
I have spread my dreams under your feet;
Tread softly because you tread on my dreams."""
print(first_Multi_Line_String + second_Multi_Line_String)