Python Forum
Find if chain of characters or number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find if chain of characters or number
#1
Hello,

Try to determine if its a chain of characters or numbers.

mot = input("input something:  ")

for num in mot:
    if num not in "0123456789":
       print("its ccharacters")
    else:
       print("its number")

      
Output:
input something: jkl its ccharacters its ccharacters its ccharacters
The problem it is checking every characters one by one and i just want to tell for the whole word.

Dont use isnumber or digit gadget.

TY
Reply
#2
Might try this. If the string has numbers and letters ir will return as a string.

while True:
    prompt = input('input: ')
    if prompt == 'q':
        break

    try:
        if int(prompt):
            print('numbers')
    except ValueError:
        print('letters')
Output:
input: 6523 numbers input: string letters input: number1 letters input: number 2 letters input: q
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
You can use "isnumeric()"
print('123'.isnumeric(), 'abc'.isnumeric(), '1.23'.isnumeric())
Output:
True False False
You can use a regular expression.
import re

is_int = re.compile(r'^-?\d*$')
print(is_int.match('123') is not None)
print(is_int.match('abc') is not None)
print(is_int.match('1.23') is not None)
print(is_int.match('-123') is not None)
Output:
True False False True
As menator01 recommends, conversion (you probably want the numeric value anyway) and catching the exception is likely the best solution. Use float() instead of int() if you want to allow decimals.
Frankduc likes this post
Reply
#4
Updated to include floats

while True:
    prompt = input('input: ')
    if prompt == 'q':
        break

    try:
        int(prompt)
        print('number')
    except ValueError:
        try:
            float(prompt)
            print('float')
        except ValueError:
            print('letters')
Output:
input: 3259 number input: 5.33 float input: text letters input: 5text letters
BashBedlam and Frankduc like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
Hello guys.

Sorry for the late reply, i had courses yesterday since i started in AI.
The book was asking for brute force approach. You cannot use try, except, import or isnumeric.

It is tempting i admit to use gadget but forbidden.

I came up with that solution in my bed at one am. Sorry its in french.

mot = input("Une chaîne de charactères svp!:  ")
count = 0

for num in mot:
    if num in "0123456789":
       count += 1
       if count == len(mot):
          print("Il s'agit d'un nombre entier de", count, "chiffres")
    else:
      print("Il s'agit d'une chaîne de caractères.")
Output:
Une chaîne de charactères svp!: 4789 Il s'agit d'un nombre entier de 4 chiffres
Thanks guys for the help. I will take in note some of your suggestion.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Chain object that have parent child relation.. SpongeB0B 10 1,078 Dec-12-2023, 01:01 PM
Last Post: Gribouillis
  find the sum of a series of values that equal a number ancorte 1 493 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,188 Apr-05-2023, 07:36 PM
Last Post: Frankduc
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,628 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,410 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  How to find the first and last of one of several characters in a list of strings? tadsss 2 2,183 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,672 May-15-2020, 01:37 PM
Last Post: snippsat
  Find the complement of a number landlord1984 4 32,794 Feb-21-2020, 04:26 AM
Last Post: shreyaspadhye3011
  Find Average of User Input Defined number of Scores DustinKlent 1 4,281 Oct-25-2019, 12:40 AM
Last Post: Larz60+
  find nearest number d3fi 7 3,939 Aug-26-2019, 09:32 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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