Thread: text cut

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

    Join Date
    Feb 2005
    Location
    NIPPON
    Posts
    15
    Rep Power
    0

    text cut


    How can you cut certain string from a varialbe like perl does ?

    perl does like this

    s = 'dddhhh';
    s =~ s/ddd//;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    in python you can simply slice the string. say you want to get only the first three letrs of a string
    Code:
    foo = "python"
    bar = foo[0:3]
    print bar
    "pyt"
    so you just assign the parts of the string you want.
    go through the Python tutorial that comes with your distrubution. its indispensable.

    Comments on this post

    • sf2k disagrees
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Code:
    mystring = "onethreetwothree"
    mystring = mystring.replace("three", "")  # mystring is now "onetwo"
    
    mystring = "onethreetwothree"
    mystring = mystring.replace("three", "", 1)  # mystring is now "onetwothree"
    --OH.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    25
    Rep Power
    0
    Lists are one of python's strongest points. If you need to cut information, you should check out the docs on the lists. They have tons and tons of methods used with them and are invaluable.

    Here is a weak example of sorting, but the same thing in java requires an entire sort methods and a lotta work. In python it can be done in just 2 lines.

    Code:
    >>> test = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> test.append(4.5)
    >>> test
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 4.5]
    >>> test.sort()
    >>> print test
    [1, 2, 3, 4, 4.5, 5, 6, 7, 8, 9, 10]

    Comments on this post

    • sf2k disagrees
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    If the part of the string you want to cut is always at the beginning or the end of the string, you can use strip()
    Code:
    >>> s = 'dddhhhddd'
    >>> s.strip('ddd')
    'hhh'
    >>> s.lstrip('ddd')
    'hhhddd'
    >>> s.rstrip('ddd')
    'dddhhh'
    Regards, mawe

    Comments on this post

    • sf2k disagrees
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can also use the replace() string method to remove a sub-string easily, regardless of it's position in the target string. Here's an example of this.

    Code:
    >>> s = 'dddhhh'
    >>> s.replace('ddd', '')
    'hhh'
    >>> s = 'dddhhhddd'
    >>> s.replace('ddd', '')
    'hhh'
    >>> s.replace('ddd', '', 1)
    'hhhddd'
    >>>
    In your post you used regular expressions. Python also supports (Perl style) regular expressions through a standard module called re.

    http://www.python.org/doc/2.4/lib/module-re.html

    Hope this helps,

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

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Location
    NIPPON
    Posts
    15
    Rep Power
    0

    arigato


    thanks Mark !!

IMN logo majestic logo threadwatch logo seochat tools logo