Python Forum
stopping number conversion before end
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
stopping number conversion before end
#2
I don't know a short-and-sweet method to do that. I would probably use a regex, since that easily stores the index of where it finds the first non-digit. But using something like itertools.takewhile or itertools.groupby with str.isdigit could also be used to split the string and rejoin the "integer" parts.


import re

s = "435xkb3"

if mo := re.match(r"(\d+)", s):   #Python 3.8 walrus operator.  Split into 2 lines for earlier versions.
    number = int(mo.group(1))
    print(f"The starting number is {number} and the rest begins at index {mo.end()}")
    print(f"the rest -> {s[mo.end():]}")
else:
    print(f"I couldn't find any integer at the start of the string")
Reply


Messages In This Thread
stopping number conversion before end - by Skaperen - Jul-09-2020, 10:48 PM
RE: stopping number conversion before end - by bowlofred - Jul-09-2020, 11:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  multiple number format conversion oli_action 4 3,529 Aug-11-2020, 05:10 AM
Last Post: perfringo
  Stopping a loop in another file catosp 4 3,732 Jun-15-2020, 02:45 PM
Last Post: catosp
  Help with Stopping a function after a timer SheeppOSU 0 2,341 Jan-28-2019, 10:13 PM
Last Post: SheeppOSU
  reading raw data until condition is met, then stopping unknowntothem 7 5,440 Sep-27-2018, 06:10 AM
Last Post: unknowntothem
  Stopping scheduler with condition j.crater 17 20,272 Jan-10-2017, 11:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020