Sorry guys...this is probably really trivial but i'm new to Python.

How do you remove the first digit from a five digit number called x and then add this new number to the original x.
I need to be able to remove any of the digits and add the new number to the original one.

Thanks for any help

Recommended Answers

All 7 Replies

Sorry guys...this is probably really trivial but i'm new to Python.

How do you remove the first digit from a five digit number called x and then add this new number to the original x.
I need to be able to remove any of the digits and add the new number to the original one.

Thanks for any help

I would change the integer to a string then strip the first character off

Member Avatar for leegeorg07

I'm not sure, without running it myself but I would say:

x=12345
print x+x[0]
#and it should print x+1
#or you could do:
for i in range(len(x)+1):
  i-=1
  print x+x[i]
#and that should produce:
#12346
#12347
#12348
#12349
#12350

once again, I'm not too sure about this

Member Avatar for leegeorg07

I would change the integer to a string then strip the first character off

I didnt thing you needed to do so with integers, that the index function handled it anyway

x=12345
print x+x[0]

This must be features on the new Python 3.1.1 because it won't work on 2.6.1

Member Avatar for leegeorg07

oh, I use 2.6.1 so I guess that I just got it wrong :( but it would be something like this then:

x=12345
tempx=str(x)
print x+int(tempx[0])
#or:
for i in len(x+1):
  i-=1
  print x+int(tempx[i])

Maybe this will work

Editor: How about testing code out before you post it as a solution.

You mean like I said in the previous post

Member Avatar for leegeorg07

yeah, I really want to research this I'll keep this thread in mind when I finish it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.