Can we perform the same operations on multi-line strings that we perform on other strings?

Question

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.

Hello, sir.
Would you be so kind to provide examples to this?
Thank you in advance.

11 Likes

I agree that examples will be helpful.

1 Like

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.

32 Likes
test = "bsh   

nd"

test_1 = 'haj 

sn'

test_2 = """hkjav

lk  jf"""

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?

1 Like

Yes, only triple quotation marks can create multi-lines comments.

3 Likes

how can i put a variable containing a int in the middle of a multi line string variable?

for example:

Assign the string here

value = 13

to_you = “”"Stranger, if you passing meet me and desire to speak to me, why

should you""" + value “”" not speak to me?

And why should I not speak to you?"""

print(to_you)
how can i get this to work, do i have to split up the multi line string into two and then concatenate?

1 Like

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
1 Like

Hi, @core0852353437,

Yes, and there is also a + operator missing from this line:

should you""" + value """ not speak to me?
1 Like

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

If you ended the “”" quotes on the same line and then concatenate with the value, it prints on the same line.

to_you = “”" Stranger, if you are passing and meet me and desire to speak to me, why

should you not speak to me?

And why should I not speak to you?"""

value = 6

print(to_you, str(value))

hi Core, this is how I did it:
this is the code:

a=10

to_you=""“Stranger, if you passing meet me at”"" + " " + str(a) + " " + “”"and speak to me, why

should you not speak to me?

And why should I not speak to you?"""

print(to_you)

This is the output:

Stranger, if you passing meet me at 10 and speak to me, why
should you not speak to me?
And why should I not speak to you?

Basically, I added (concatenate) spaces[" "] and integer as a string [str(a)] in between multi-line string.

p.s. still very new at this

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)
2 Likes

Thanks for the example @tracychacon

1 Like

Thanks @catower for sharing this information :upside_down_face:

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.

By the way, thanks for asking. Happy learning/coding!

what if you want the response the second_Multi_Line_String to start on a new line ?

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)
1 Like