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

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    This might be a challenge: I need some help with slicing a raw_input


    Hi!
    I am new to programming and have learned recently how to slice.
    I am practicing my writing my own code similar to pyg latin except by taking the inputted word in raw_input I need to check the last character and I dont know how to index an unknown length string.

    for example
    word=raw_input("enter word:")
    ....the string "paper" is entered

    I need my code to identify the last character of the string

    I thought of only two solutions

    1. write a bajillion if/elif statements based
    if len(word)<4 and len(word)<6
    then i would index the last character as word[4]
    this would give me the letter "r" in paper

    this is however long and tedious and could be never ending unless i figure out how to write a code with count statements and I could do this once. but I'm new, like i said, and have a lot of difficulty using count statements as is...never mind something complicated

    and

    2. something like slicing word[2:] which would print out the characters second through the end
    except im looking for just the end
    is there anything like the function word[last_character]
    or word[end]??

    I'd really appreciate anyone's help
    I'm doing this for fun and just some practice getting the hang of programming.

    Thanks!
    -Kristie
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    You can use negative indexes. The index [-1] refers to the last element of a sequence:
    Code:
    "paper"[-1]
    -Mek

IMN logo majestic logo threadwatch logo seochat tools logo