Search code examples
pythonprintingoutputline-breaks

How I can do break a line in output print python


I´m new in Python and I´ve a code that extract cookies from HTTP Headers and this is Ok. But I need break line on output to friendly format.

The output is like this:

has_recent_activity=1; path=/; expires=Sat, 31 Aug 2019 16:24:07 -0000,_octo=GH1.1.1398580293.1567265047; domain=.github.com; path=/;expires=Tue,31 Aug 2021 15:24:07 -0000, logged_in=no; domain=.github.com; path=/;expires=Wed, 31 Aug 2039 15:24:07 -0000; secure; HttpOnly"

And I need format like this:

has_recent_activity=1; path=/, 
Session=GH1.1.1398580293.1567265047; domain=.github.com; path=/,
logged_in=no; domain=.github.com; path=/; secure; HttpOnly

Break line for each cookie tag

This is my simple code:

import urllib2

r = urllib2.Request("https://github.com/carineconstantino/security_tools")

c = urllib2.urlopen(r)

cookies = c.info()['Set-Cookie']

print (cookies)

Solution

  • Assuming I am interpreting your question correctly, you are trying to add line breaks in certain parts of a string you're trying to print.

    One approach (although there may perhaps be better ones) would be to swap the

    print(cookies)

    for something along the lines of

    print(cookies.replace('path=/; ', 'path=/;\n'))

    or

    print(cookies.replace('path=/, ', 'path=/,\n'))

    if you prefer the line break after the path=/,

    Line breaks in print statements can generally be handled with \n.