0
How to identity the type of data input
13 Antworten
+ 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 :)
+ 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
+ 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.
+ 2
Thanks
+ 1
a = input()
print(type(a))
+ 1
no ,every time the result is a int
+ 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?
+ 1
I want to check the type of prompted input
+ 1
Well ok. You get the type via type(a). So where is the problem?
I don't really get it. I'm sorry 😔
+ 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"
+ 1
can we do this without using regex
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 :)
0
Ah ok, that's how it was meant.
Sure, before converting you should check it first. 😅