0

I'm having issues skipping or trimming the first two numbers in a provided argument. As an example I am passing the value of "00123456" to 'id'. I want the request.args.get against 123456 instead 00123456. is there a function I can use to drop off the zero's. Also relatively new to the python world so please advise if I need to provide more info.

@main.route('/test')
def test():
    """
    Test route for validating number
    example - /test?id=00123456
    """

    # Get number passed in id argument
    varNum = request.args.get('id')
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • This was the first of many google results for "python trim zeros". Please read [How much research effort is expected of Stack Overflow users?](//meta.stackoverflow.com/a/261593/843953) Since you're new, please also read the following helpful links: [tour], [what's on-topic here](/help/on-topic), [ask], the [question checklist](//meta.stackoverflow.com/q/260648/843953), and how to provide a [mre] – Pranav Hosangadi Feb 15 '22 at 23:22

2 Answers2

1

You can convert the "00123456" to an int and it will remove all the zeros at the start of the string.

print(int("00123456"))

output:

123456

Edit:

Use this only if you want to remove any zeros at the start of the number, if you want to remove the first two chars use string slicing.

Also, use this only if u know for sure that the str will only contain numbers.

omercotkd
  • 493
  • 1
  • 6
  • 13
0

You can use string slicing, if you know that there are always two zeroes:

varNum = request.args.get('id')[2:]

Alternatively, you can use .lstrip(), if you don't know how many leading zeroes there are in advance:

varNum = request.args.get('id').lstrip('0')
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • 1
    Questions as basic as this have usually been asked and answered. Since the community prefers for answers to the same question to be consolidated, it's better to look for a duplicate target and vote to close :) – Pranav Hosangadi Feb 15 '22 at 23:20
  • Thank you everyone. I went the route of using string slicing as it will always be two zeros. – dgonzalesjr Feb 16 '22 at 00:29