0

I have a string:

i = '12345678901'

How to check (with python) if one before last digit in a string is even or odd ?

In a string above I want function to return or print "even".
I found some examples but they are in C and i don't know how to use it in Python

Chris
  • 517
  • 1
  • 9
  • 22
  • print('even' if (-1)**i==1 else 'odd') I know how to check for a number if it's odd or even but how can I extract one before last digit – Chris Jan 13 '13 at 23:53
  • 1
    @Chris: that's an unusual way to do that. – Eric Jan 13 '13 at 23:54
  • Slice it up and go from there – Peter Wooster Jan 13 '13 at 23:56
  • 2
    See: http://www.tutorialspoint.com/python/python_strings.htm (mainly, string splicing). `i[-2]` returns '0', which is a string. We need to convert this to an integer: `int(i[-2])`. Now, to check if it is even or odd: `if int(i[-2]) % 2 == 0: return 'Even'`. `else: return 'Odd'` `%` is the modulus operator. It returns the remainder of the division of the first element and the second element. – Rushy Panchal Jan 13 '13 at 23:57

7 Answers7

3

There are (at least) two ways of doing this:

Method 1:

Get the character at index -2 and intify it and check its modulus with 2

In [119]: i = '12345678901'

In [120]: int(i[-2])%2
Out[120]: 0

In [121]: int(i[-1])%2
Out[121]: 1

Method 2:

intify i, and divide by 10 and mod by 10 to get at the second last digit and nod by 2 get determine if it's even or odd

In [122]: i = 12345678901

In [123]: (i/10)%10
Out[123]: 0

In [124]: ((i/10)%10)%2
Out[124]: 0
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
2

A one-liner:

f = lambda s: 'odd' if int(s[-2]) % 2 else 'even'

Tested on input:

>>> i = '12345678901'
>>> f(i)
'even'
>>> i = '12345678931'
>>> f(i)
'odd'
ezod
  • 7,261
  • 2
  • 24
  • 34
1

This is pretty easy to achieve, you just need a tiny bit of slicing, the modulus operator(%) and you're done.

i = '12345678901'

second_last = i[-2]

if int(second_last) % 2 == 0:
    print("Even")
else:
    print("Odd")

Output:

Even
Marius
  • 58,213
  • 16
  • 107
  • 105
0
i = '12345678901'
print int(i[-2]) % 2 == 1
# False

For it to print even or odd, you need to do this:

if int(i[-2]) % 2 == 0:
    print 'Even'
else:
    print 'Odd'

You could also use the fact that 1 is interpreted as True by if statements (and 0 is False):

if int(i[2]) % 2:  #if int(i[-2]) % 2 returns 1
    print 'Odd'
else:
    print 'Even'
Volatility
  • 31,232
  • 10
  • 80
  • 89
0

Step 1: Get second last digit:

last_digit = i[-2]

Step 2: is it odd?

is_odd = last_digit in '13579'
Eric
  • 95,302
  • 53
  • 242
  • 374
0

This starts with taking a slice of the data, see Explain Python's slice notation for a good explanation of slice. The official documentation isn't that obvious.

You can use negative numbers to slice from the end. Once you have the slice you need to check if it is a digit, find its nmeric value and see if it's even.

Coding s left as an exercise for the reader.

Community
  • 1
  • 1
Peter Wooster
  • 6,009
  • 2
  • 27
  • 39
0
(i % 10) & 1

returns 1 if odd and 0 if even

code -

print "even" if not ((int(i) / 10) % 10) & 1 else ""
Confuse
  • 5,646
  • 7
  • 36
  • 58