String functions

Discussion in 'Python' started by Kali K E, Sep 13, 2003.

  1. Kali K E

    Kali K E Guest

    Hi,

    I want two simple things.

    1. To find the length of the string
    2. To find the contants of the last character in a string. I tried to
    use $ for that, but I am getting syntax error. I do not know the
    correct format. Basically I have to check
    if mystring[lastchr] == '.':
    dosomething
    else:
    donothing

    Please help me.
     
    Kali K E, Sep 13, 2003
    #1
    1. Advertising

  2. On 13 Sep 2003 12:21:30 -0700, Kali K E <> wrote:

    > Hi,
    > I want two simple things.
    >
    > 1. To find the length of the string


    >>> str = 'i am a string'
    >>> len(str)

    13

    > 2. To find the contants of the last character in a string.


    >>> str[-1]

    'g'

    > Basically I have to check
    > if mystring[lastchr] == '.':
    > dosomething
    > else:
    > donothing


    if str[-1] == '.':
    dosomething
    else:
    donothing

    HTH,

    J.
     
    Jordan Krushen, Sep 13, 2003
    #2
    1. Advertising

  3. "Kali K E" <> wrote in message
    news:...
    > Hi,
    >
    > I want two simple things.
    >
    > 1. To find the length of the string


    Use the len function:

    >>> s = "Hi there!"
    >>> print s

    Hi there!
    >>> len(s)

    9
    >>>


    > 2. To find the contants of the last character in a string. I tried to
    > use $ for that, but I am getting syntax error. I do not know the
    > correct format. Basically I have to check
    > if mystring[lastchr] == '.':
    > dosomething
    > else:
    > donothing
    >
    > Please help me.


    Use the indexer. Python sequences (lists, tuples, strings) can take negative
    numbers as indexes. If you use a negative number, it starts counting from
    the end of the sequence. So, to get the last character, do:

    >>> print s[-1]

    !
    >>>


    -Chris
     
    Chris Tavares, Sep 13, 2003
    #3
  4. Kali K E wrote:

    > if mystring[lastchr] == '.':
    > dosomething
    > else:
    > donothing


    #v+

    >>> s = 'abc'
    >>> s.endswith('c')

    True
    >>> s.endswith('z')

    False
    >>>


    #v-

    // Klaus

    --
    ><> unselfish actions pay back better
     
    Klaus Alexander Seistrup, Sep 13, 2003
    #4
  5. Jordan Krushen wrote:
    > On 13 Sep 2003 12:21:30 -0700, Kali K E <> wrote:
    >>... Basically I have to check
    >> if mystring[lastchr] == '.':
    >> dosomething
    >> else:
    >> donothing

    > if str[-1] == '.':
    > dosomething
    > else:
    > donothing


    The idiom you want is probably:
    if somestr.endswith('.'):
    dosomething
    else:
    donothing

    (1) don't use str as a variable name, you'll mask the builtin str.
    (2) the above code raises no exception if somestr is length 0.
    ''[-1] raises an exception, but ''.endswith('whatever') is False.

    -Scott David Daniels
     
    Scott David Daniels, Sep 14, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    11
    Views:
    830
    tshad
    May 27, 2005
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,629
    Victor Bazarov
    Jun 21, 2004
  3. korean_dave
    Replies:
    2
    Views:
    324
    John Machin
    Jun 17, 2008
  4. Tim Slattery

    String functions run on empty string

    Tim Slattery, Jul 11, 2007, in forum: ASP General
    Replies:
    1
    Views:
    140
    Tim Slattery
    Jul 11, 2007
  5. Replies:
    2
    Views:
    460
Loading...

Share This Page