0

Very new to python here. How do you split a very long dictionary value over two lines while still having it appear as one line when it is output with print()? See code below.

glossary = {'dictionary' : 'A mutable associative array (or dictionary) of key and value pairs. Can contain mixed types (keys and values). Keys must be a hashable type'}

I've tried using triple quotes (i.e. """) with no success since I don't think the value is technically a string.

Quarksta
  • 3
  • 2
  • You mean a very large value? Triple quotes do work, but you'll want to escape the newlines. – 9769953 May 24 '22 at 07:16
  • I think this has nothing to do with printing, but with correct indentation, you can still make your value as two lines or more –  May 24 '22 at 07:18

1 Answers1

2

you can use \ (backslash) to continue code onto the next line

e.g.

print("hello \
how are you")

would return

hello how are you

edit: you should be able to use """ as (from my understanding) it just converts it to a normal string but adds the line breaks. this wouldnt give the result you wanted but it should work

edit: just tested the above:

list = ['hi', '''thing1
thing2''']
print(list)

ouput:

['hi', 'thing1\nthing2']

that \n means newline so i would use the backslash as i mentioned above if you want the correct output

Peter W
  • 36
  • 1
  • 3
  • \ (backslash) worked but affects the ability to follow the PEP style guide when there are multiple dictionary entries. If you indent the second line, the tab space shows up when you use print(). – Quarksta May 24 '22 at 07:25
  • ''' is the way to go if I want to indent the second line with tabs. I had been using """. Thank you for your help. glossary = {'dictionary' : '''A mutable associative array (or dictionary) of key ''' '''and value pairs. Can contain mixed types (keys and values). ''' '''Keys must be a hashable type'''} – Quarksta May 24 '22 at 07:35