Thread: limiting print

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Raleigh NC
    Posts
    89
    Rep Power
    10

    limiting print


    if i am writing alll of a portion of a file however on screen i only want to print a short description such as 13 characters how would i do this?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Here are a couple of ways to do it:

    1. Make a substring out of the string and print that:
    Code:
    foo = "This is a rather long string of characters"
    print foo[0:13]
    2. Use the formatting function to only print 13 characters:
    Code:
    foo = "This is a rather long string of characters"
    print "%.13s" % foo
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Raleigh NC
    Posts
    89
    Rep Power
    10
    gracias
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    To expand here. If you want to do this for every line in the file then you'd end up with something like this. Personally I'd go with the slice format .

    Code:
    ...
    
    for line in file('sample.txt'):
        print line[:13]
    ...
    Note: that you don't need the 0 index in the slice, this isn't any big change but its nice to know.

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    netytan: Why dont you try and get out of the python forum for a change will ya buddy
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Smile


    netytan is the best!
    xlordt you need to update your links!
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    It's good to be loved hehe . Thanks guys it means a lot to me .
    programming language development: www.netytan.com Hula

  14. #8
  15. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by Dietrich
    netytan is the best!
    xlordt you need to update your links!
    yap, just one link.. will do it when im not lazy
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Originally Posted by xlordt
    yap, just one link.. will do it when im not lazy
    that could take a while...
  18. #10
  19. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by CyBerHigh
    that could take a while...
    seems like you been there before

IMN logo majestic logo threadwatch logo seochat tools logo