I am learning python - Why Single quotation marks and code syntax is printed in my terminal?

Why Single quotation marks and code syntax is printed in my terminal?

I wrote this code in my tutorial file:

var1 = "hello"
print(var1)
print(type(var1))

var2 = """Peter Piper picked a peck of pickled peppers.

A peck of pickled peppers Peter Piper picked.

If Peter Piper picked a peck of pickled peppers?

Where’s the peck of pickled peppers Peter Piper picked?
"""+"""              The""",var1,""" end"""
print(var2)

But when I run my code in my terminal It showed this:


hello
<class 'str'>
('Peter Piper picked a peck of pickled peppers.\n\nA peck of pickled peppers Peter Piper picked.\n\nIf Peter Piper picked a peck of pickled peppers?\n\nWhere’s the peck of pickled peppers Peter Piper picked?\n              The', 'hello', ' end')

I wanted it to print this:

hello
<class 'str'>
Peter Piper picked a peck of pickled peppers.

A peck of pickled peppers Peter Piper picked.

If Peter Piper picked a peck of pickled peppers?

Where’s the peck of pickled peppers Peter Piper picked?
                  The hello end

I don't know how to fix it Please help me

I have 3.8.6 Python interpreter

3 answers

  • answered 2021-04-08 03:12 Michael Ruth

    You have defined var2 as a tuple rather than str. To see this for yourself, print the type of var2: print(type(var2)), you'll see that it is a tuple.

    Python allows tuples to be defined three ways:

    1. tuple(sequence)
    2. (a, b, c)
    3. a, b, c

    I avoid using the third syntax because it's very easy to edit unintentionally.

    You need to replace the ,s in var2's declaration with +s in order to perform concatenation:

    var2 = """Peter Piper picked a peck of pickled peppers.
    
    A peck of pickled peppers Peter Piper picked.
    
    If Peter Piper picked a peck of pickled peppers?
    
    Where’s the peck of pickled peppers Peter Piper picked?
    """+"""              The""" + var1 + """ end"""
    

  • answered 2021-04-08 03:14 leaf_yakitori

    you can try this

    var1 = "hello"
    print(var1)
    print(type(var1))
    
    var2 = f"""Peter Piper picked a peck of pickled peppers.
    
    A peck of pickled peppers Peter Piper picked.
    
    If Peter Piper picked a peck of pickled peppers?
    
    Where’s the peck of pickled peppers Peter Piper picked?
                        The {var1} end"""
    print(var2)
    

    result:

    hello
    <class 'str'>
    Peter Piper picked a peck of pickled peppers.
    
    A peck of pickled peppers Peter Piper picked.
    
    If Peter Piper picked a peck of pickled peppers?
    
    Where’s the peck of pickled peppers Peter Piper picked?
                  The hello end
    

  • answered 2021-04-08 03:22 sh.e.salh

    String concatenation done with the "+" operator so just change your last line to:

    """+"""              The """+var1+""" end"""
    

    using "," means you are creating a tuple.