Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detect if an integer ends in 0 or 5
I am new to Python.  How do I determine if a variable (it will always be a two or three digit integer) ends either in a '0' or a '5'.  If it does, I want to print it (I know how to do the print part).

thanks for looking
>>> n = '125'
>>> n.endswith('5')
if it is of type int - check that reminder of division by 5 is 0.
if it is str - chek the last char to be in ['0','5']
micseydel and metulburr like this post
Snippsat -- my variable will always be a two or three digit integer -- will your solution work? (I tried it with my variable and received a "Attribut Error": 'int' object has no attribute 'endswith'.

buran - will give your solution a shot -- once I go back to manual and figure out how to check for a remainder.

thanks to both for responding

Burman -- got it --- thanks
my_integers = [1,20,32,145,210,103]

for num in my_integers:
    if not num%5:
        print num
#list comprehension
nums_ending05 = [n for n in my_integers if not n%5]
print nums_ending05

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Convert element of list to integer(Two dimentional array) zorro_phu 3 81 Jun-12-2018, 04:49 AM
Last Post: zorro_phu
  detect if sys.stdin is ... Skaperen 4 126 May-30-2018, 11:49 PM
Last Post: Skaperen
  Changing User Input Strings Into Set Integer Values Mario128843 3 104 May-01-2018, 03:32 AM
Last Post: Mario128843
  string with integer variable within Andy1845c 9 215 Mar-12-2018, 10:28 PM
Last Post: Larz60+
  Convert integer to string Maigol 5 806 Mar-05-2018, 10:25 PM
Last Post: python_master89
  How to check if user entered string or integer or float?? prateek3 4 229 Feb-26-2018, 05:46 PM
Last Post: prateek3
  storing input as a integer help faputting 1 93 Feb-23-2018, 06:39 PM
Last Post: nilamo
  datetime.date() - TypeError: an integer is required (got type str) bisoftware 7 2,010 Jan-30-2018, 08:05 PM
Last Post: buran
  About generating N integer numbers without repetition Otbredbaron 3 268 Jan-30-2018, 12:08 PM
Last Post: Otbredbaron
  Detect comments part using web scraping seco 7 324 Jan-18-2018, 10:06 PM
Last Post: seco

Forum Jump:

Users browsing this thread: 1 Guest(s)