1,105,312 Community Members

How do I get the LAST TWO character in a string?

Member Avatar
G_S
Junior Poster
142 posts since Mar 2010
Reputation Points: 38 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello

I'm trying to make a verb conjugator for Spanish. The problem is that I need to get the last two characters in the verb to be able to determine the verb group to which it belongs.

In Spanish, conjugation depends on the verb ending, among other factors. Verbs end in either -ar, -er or -ir. So the program must be able to isolate these two letters and then use conditional structures to determine how to proceed.


But here is the problem:

verb = input("Write the verb here: ")
if verb[-2:-1] == 'ar':
    do this
elif verb[-2:-1] == 'er':
    do this
elif verb[-2:-1] == 'ir':
    do this
else:
    print('you did not provide a verb')

if verb is 'amar', verb [-2:-1] will return 'a', not 'ar'. Can you tell me a way of telling python to isolate from the second to last character up to the VERY last one?


After much thinking I came up with this:

verb= 'amar'
verb=[-2:len(verb)]
'ar'

But I don't know if this is the right wayor if this is elegant at all.

Please help me.

Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

You can write verb[-2:] . Another helpful function is verb.endswith("ar") which returns True or False. Also read the documentation on string methods http://docs.python.org/library/stdtypes.html#string-methods

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

The official way is data[-2:]

Member Avatar
woooee
Posting Maven
2,792 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
1
 

Also, form the habit of using a list instead of if/elif. It is more straightforward as all of the conditions are on one line. And verb[-2:] is just shorthand for verb[-2:len(verb)], just as verb[:-2] is shorthand for verb[0:-2].

verb = input("Write the verb here: ")
if verb[-2:] in ['ar', 'er', 'ir']:
#
else:
#
# or
found = False
for ending in ['ar', 'er', 'ir']:
    if verb.endswith(ending):
        found = True
Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

Also, form the habit of using a list instead of if/elif. It is more straightforward as all of the conditions are on one line. And verb[-2:] is just shorthand for verb[-2:len(verb)], just as verb[:-2] is shorthand for verb[0:-2].

verb = input("Write the verb here: ")
if verb[-2:] in ['ar', 'er', 'ir']:
#
else:
#
# or
found = False
for ending in ['ar', 'er', 'ir']:
    if verb.endswith(ending):
        found = True

Second case shortly can be said:

found = verb.endswith(['ar','er','ir'])
Member Avatar
richieking
Posting Shark
926 posts since Jun 2009
Reputation Points: 40 [?]
Q&As Helped to Solve: 171 [?]
Skill Endorsements: 3 [?]
 
0
 
for((word  = wordlist[-2]) in ['ar','er',ir']):
    print word
Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
1
 
for((word  = wordlist[-2]) in ['ar','er',ir']):
    print word

Riechieking, you should test your code before posting. This can't work in python because word = wordlist[2] is not an expression but a statement. This is a C idiom, not a python one.

Member Avatar
richieking
Posting Shark
926 posts since Jun 2009
Reputation Points: 40 [?]
Q&As Helped to Solve: 171 [?]
Skill Endorsements: 3 [?]
 
0
 

Riechieking, you should test your code before posting. This can't work in python because word = wordlist[2] is not an expression but a statement. This is a C idiom, not a python one.

Grib your are right. Its a C++ style. I just send that from my htc phone and there was no time to test.
I will check it up one I get home.
Cheers

Member Avatar
G_S
Junior Poster
142 posts since Mar 2010
Reputation Points: 38 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you all for your quick answers!

The endswith method is going to be really helpful for my project.

I'm going to test all your suggestions and will post back if more questions arise (I'm intrigued by the way you used the lists).

Question Answered as of 2 Years Ago by richieking, pyTony, Gribouillis and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article