How to identity the type of data input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to identity the type of data input

15th Jul 2018, 9:41 AM
Sonal Ingle
Sonal Ingle - avatar
13 Answers
+ 1
It does not output an error but the type class. What you are talking about sounds more like you need regex (regular expressions). You can import this via import re This will come in one of the lessons in the python course you are doing :)
15th Jul 2018, 10:20 AM
Matthias
Matthias - avatar
+ 3
Matthias suppose you are taking input as in = int(input()) then if user enters any character rather than number, then it cann't typeclass, you will get an error. Sonal Ingle yes in a way you can do it using regex, regex is used to find a specific pattern inside a string
15th Jul 2018, 10:29 AM
Nikhil Dhama
Nikhil Dhama - avatar
+ 2
I think you can not know exactly what type of data is inputted by user, unless you used it, or you prints it, because input() with always returns the input as a string, but may be there is a way what you are trying to do.
15th Jul 2018, 10:24 AM
Nikhil Dhama
Nikhil Dhama - avatar
+ 2
Thanks
15th Jul 2018, 10:31 AM
Sonal Ingle
Sonal Ingle - avatar
+ 1
a = input() print(type(a))
15th Jul 2018, 9:45 AM
Matthias
Matthias - avatar
+ 1
no ,every time the result is a int
15th Jul 2018, 9:53 AM
Sonal Ingle
Sonal Ingle - avatar
+ 1
Actually it's a string, when I use the Sololearn playground. You can play around and output the type of different variables. If you want the input to be an integer, you can cast it via a = int(a) You can also use the type() to check if it has the dessired type or not. What exactly do you want to do?
15th Jul 2018, 9:57 AM
Matthias
Matthias - avatar
+ 1
I want to check the type of prompted input
15th Jul 2018, 10:00 AM
Sonal Ingle
Sonal Ingle - avatar
+ 1
Well ok. You get the type via type(a). So where is the problem? I don't really get it. I'm sorry 😔
15th Jul 2018, 10:02 AM
Matthias
Matthias - avatar
+ 1
with the code given by you, the output of an alphabet is not coming as a string rather an error is coming I want a code that: inputs an alphabet and outputs as "This is an alphabet " inputs a number and outputs as "This is a number" and a special character entered as"This is a special character"
15th Jul 2018, 10:09 AM
Sonal Ingle
Sonal Ingle - avatar
+ 1
can we do this without using regex
15th Jul 2018, 10:21 AM
Sonal Ingle
Sonal Ingle - avatar
0
If coarse distinction between string and int would be enough then yes. Or you could check sepperately for every special character if it is a string. Like if(a=='%' or a=='&' ....) and so on Regex makes this a lot easier, so I would really recommend to have a look into this topic. It looks difficult at first, but when you are getting used to it, it gives you a lot :)
15th Jul 2018, 10:26 AM
Matthias
Matthias - avatar
0
Ah ok, that's how it was meant. Sure, before converting you should check it first. 😅
15th Jul 2018, 10:31 AM
Matthias
Matthias - avatar