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

    Join Date
    May 2004
    Posts
    180
    Rep Power
    11

    Need to know if there is a python function to...


    I need to return x number of chars in a string. Is there a function for this?

    Thanks.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Nope, there isn't a function. Just treat the string as if it were a list:
    Code:
    str = "This is a test"
    str2 = str[2:10]
    print str
    print str2
    See? Who needs a function when python has a built-in operator for this
    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. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    17
    and if you just mean "how many chars are there in a specified string":

    Code:
    str = "This is a test"
    length = len(str)
    Last edited by deltacoder; October 19th, 2004 at 02:29 AM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    23
    Rep Power
    0
    use slice.
    eg:

    str = "my string"
    print str[:2]

    will print my

IMN logo majestic logo threadwatch logo seochat tools logo