Search code examples
pythonjsonimdb

How to print the value of a variable in multiple lines in python?


I am writing the plot of a movie from OMDB API. I want to limit the plot value to be within 80 length in the output. I have searched but, could not found anything. The jsonvalues['Plot'] can contain more than 80 characters. I want to print its value in multiple lines.

import urllib
import json

name = raw_input('Enter the movie name >')
url = "http://www.omdbapi.com/?t="+name+"&r="+"json"
response = urllib.urlopen(url).read()
jsonvalues = json.loads(response)

if jsonvalues["Response"]=="True":
    print jsonvalues["imdbRating"]
    print 'The plot of the movie is: '+jsonvalues['Plot']
else:
    print "The movie name was not found"

Solution

  • Check out if this is you wanted,

    In [408]: import textwrap
    In [409]: s = "This is a long line. "*15
    In [410]: w = 75 # width
    In [411]: print(textwrap.fill(s, w))
    This is a long line. This is a long line. This is a long line. This is a
    long line. This is a long line. This is a long line. This is a long line.
    This is a long line. This is a long line. This is a long line. This is a
    long line. This is a long line. This is a long line. This is a long line.
    This is a long line.
    

    There are all kinds of functions in textwrap module. Check them out.