Python Forum
How do I check if the first X characters of a string are numbers?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I check if the first X characters of a string are numbers?
#1
How do I check if the first X characters of a string are numbers?

For example if someone inputs a combination of numbers and characters, how can I check to see if the first 3 characters are numbers?
Reply
#2
You could use string splice and isdigit
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Or a regular expression.

https://docs.python.org/3/library/re.html

import re

for string in ('123ab', '12.3ab', '12', '123', 'a123'):
    print(string, re.match(r'\d{3}.*', string))
Output:
123ab <re.Match object; span=(0, 5), match='123ab'> 12.3ab None 12 None 123 <re.Match object; span=(0, 3), match='123'> a123 None
Reply
#4
OK I figured it out.

String splice + check. (menator01's solution)

Simple.

Thanks for the replies.

All the best.
Reply
#5
menator's solution doesn't work if the string is shorter than 3 characters.
for string in ("123ab", "12.3ab", "12", "123", "a123"):
    print(string, string[:3].isdigit())
Output:
123ab True 12.3ab False 12 True 123 True a123 False
12 passes as a string that starts with 3 digits.
Reply
#6
Could use an extra condition check

for string in ('123ab', '12.3ab', '12', '123', 'a123'):
    if string[:3].isdigit() and len(string[:3]) >= 3:
        print(string)
output:
Output:
123ab 123
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
Minor point that hopefully does not apply - neither solution works for negative numbers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  doing string split with 2 or more split characters Skaperen 22 2,520 Aug-13-2023, 01:57 AM
Last Post: Skaperen
  Pulling Specifics Words/Numbers from String bigpapa 2 760 May-01-2023, 07:22 PM
Last Post: bigpapa
  Find and Replace numbers in String giddyhead 2 1,240 Jul-17-2022, 06:22 PM
Last Post: giddyhead
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,220 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Extract continuous numeric characters from a string in Python Robotguy 2 2,643 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 3,814 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  how to check if string contains ALL words from the list? zarize 6 7,224 Jul-22-2020, 07:04 PM
Last Post: zarize
  Finding line numbers starting with certain string Sutsro 3 2,545 Jun-27-2020, 12:36 PM
Last Post: Yoriz
  How to get first two characters in a string scratchmyhead 2 2,092 May-19-2020, 11:00 AM
Last Post: scratchmyhead
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,730 May-15-2020, 01:37 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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