-2

In python I have string examples as following:

str1= "True or False and True"


str2="6.4 * 32 + 43 -21+(3.4 + 2.0)"

now I want to learn both types of all elements of strings and learn the value of whole string

1)

for a in str1.split():
  #write some code here and this will print the following output

#print(a and exact_type) 
# a is True and type is bool
# a is False and type is bool
# a is True and type is bool
# and value of str1 is True type is bool


for b in str2.split():
   # write some code here and this will print the following output
# print(a and exact_type)
# b is 6.4 and type is float
# b is 32 and type is integer
# b is 43 and type is integer

#value of str2 is 232.20000000000002 and type is float

I know all of them is of type string but I want to learn possible type, pure type

edit: clarification: i am trying to find a value of bunch of strings that is given me in this form. for example, str3="True + 34" will give me error. because it can not be evaluated. in string i could have various objects. integer, float, boolean, variable that is defined before etc. since all of them in a string I could not learn type of objects. I know all substring is of type string but I should get the type ignoring object is in a string

Tarık
  • 1
  • 6
  • a string is a string what do you mean here with "possible type". And if you want to change the splitted string to integer or float do a type conversion specifically of every element after split – think-maths Dec 25 '20 at 08:25
  • 2
    Hi! Welcome to SO. It is easy to gauge that this might be an assignment problem. Please add what you have tried. – sotmot Dec 25 '20 at 08:26
  • Also, do go through previous, related question that have answers already. https://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-float – sotmot Dec 25 '20 at 08:27
  • @sotmot i am trying to find a value of bunch of strings that is given me in this form. for example, str3="True + 34" will give me error. because it can not be evaluated. in string i could have various objects. integer, float, boolean, variable that is defined before etc. since all of them in a string I could not learn type of objects. I know all substring is of type string but I should get the type ignoring object is in a string – Tarık Dec 25 '20 at 08:50

2 Answers2

1

DISCLAIMER: eval() is a very high security risk function and usually a huge code smell.

Also I hate to x-y problem you, but depending on what you're using this for, there's probably a easier and less smelly solution.

That said, you could try

for b in str1.split():
    try:
        run = eval(b)
        print(f"{b} is type of {type(run)}") 
    except (NameError, SyntaxError, TypeError):
        print(f"{b} could not be defined") 

for the first string you get:

True is type of <class 'bool'>
or could not be defined
False is type of <class 'bool'>
and could not be defined
True is type of <class 'bool
9 Guy
  • 176
  • 3
  • 14
0

I think you may want this:

s = "True or False and True"
val = eval(s)
print val, type(val)
Booster
  • 1,650
  • 13
  • 18