473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to find the type ...

Lad
Hello
How can I find out in Python whether the operand is integer or a
character and change from char to int ?
Regards,
L.

Dec 9 '05 #1
9 1733
>>> thisisastring = "1"
thisisanint = 1
type(thisisastring) <type 'str'> type(thisisanint) <type 'int'> thisisastring = int(thisisastring)
thisisanint = str(thisisanint)
type(thisisastring) <type 'int'> type(thisisanint) <type 'str'>


Dec 9 '05 #2
Lad wrote:
Hello
How can I find out in Python whether the operand is integer or a
character and change from char to int ?


I'm not sure what you mean by "character" in a Python context.
A string? "i = int(i)" will make sure both 5 and "5" are used
as 5, and "five" will be rejected with a ValueError.
def f(x): .... i = int(x)
.... print i, type(i)
.... f(5) 5 <type 'int'> f('42') 42 <type 'int'> f('infinity')

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f
ValueError: invalid literal for int(): infinity
Dec 9 '05 #3
Lad wrote:
Hello
How can I find out in Python whether the operand is integer or a
character and change from char to int ?
Regards,
L.

Easiest would just be to apply the int() type function to whatever you
have and trap any resulting exception.
getInt(1) 1 getInt("32767") 32767 getInt('have a banana') Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in getInt
ValueError: getInt called with non-integer value

Unfortunately we should also consider:

getInt("3.14159") Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in getInt
ValueError: getInt called with non-integer value getInt(3.14159) 3


which may or may not be what you want.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 9 '05 #4
ro***********@gmail.com wrote:
thisisastring = "1"
thisisanint = 1
type(thisisastring)
<type 'str'>
type(thisisanint)
<type 'int'>
thisisastring = int(thisisastring)
thisisanint = str(thisisanint)
type(thisisastring)
<type 'int'>
type(thisisanint)
<type 'str'>

print repr(thisisastring) 1 print repr(thisisanint) '1' thisisastring = 'a'
thisisanint = 98
thisisastring = ord(thisisastring) #Using ASCII rep.
thisisanint = chr(thisisanint)
type(thisisastring) <type 'int'> type(thisisanint) <type 'str'> print repr(thisisastring) 97 print repr(thisisanint)

'b'


Dec 9 '05 #5
Lad wrote:
How can I find out in Python whether the operand is integer or a
character and change from char to int ?


Python doesn't have a separate character type, but if you want to
convert a one-character string to it's ASCII number, you can use ord():
ord('A'), ord('z')

(65, 122)

The answer to your first question is that you probably don't want to.
You probably want two separate functions, one that takes an integer and
one that takes a character. What's your actual function look like?

STeVe
Dec 9 '05 #6
Lad wrote:
Hello
How can I find out in Python whether the operand is integer or a
character and change from char to int ?
Regards,
L.

You may want to try the "type" command.
And there is no character type in cPython (unless you're using ctypes
that is)

There is not much point though, you can use the "int" construct on your
expression, Python'll try to convert the expression to an integer by
itself (and throw an exception if it can't)
a = 3
int(a) 3 a = "3"
int(a) 3 a = "e"
int(a)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in -toplevel-
int(a)
ValueError: invalid literal for int(): e
You can even do base conversions with it:
a="0xe"
int(a)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in -toplevel-
int(a)
ValueError: invalid literal for int(): 0xe int(a,16) # Silly me, 0xe is not a decimal 14

Dec 9 '05 #7
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
> "Lad" == Lad <py****@hope.cz> writes:

Lad> How can I find out in Python whether the operand is integer or a
Lad> character and change from char to int ?

In Python, the canonical way of doing this would be to simply assume
that the argument can be converted to an integer and catch any errors
that occur:

def f(x):
try:
x = int(x)
except ValueError:
# It's a non-number string.
do stuff
except TypeError:
# It's neither a number nor a string.
do some other stuff

Martin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

iEYEARECAAYFAkOZwtkACgkQYu1fMmOQldXEzACgqdDVvx29UB VSIfQWnGRiAAk9
xPsAn0yN5jWrUN+6SKIHdwtILRBVyQwR
=HZQq
-----END PGP SIGNATURE-----
Dec 9 '05 #8
Xavier Morel wrote:
You can even do base conversions with it:
a="0xe"
int(a) Traceback (most recent call last):
File "<pyshell#7>", line 1, in -toplevel-
int(a)
ValueError: invalid literal for int(): 0xe int(a,16) # Silly me, 0xe is not a decimal 14

Or even say "look to the text for a clue about the base":
int('0xe', 0) 14 int('010', 0) 8 int('10', 0)

10

--
-Scott David Daniels
sc***********@acm.org
Dec 9 '05 #9
Lad
Thank you ALL for help and explanation
Regards,
L.

Dec 10 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mayer | last post by:
Hello: I'm running python under cygwin and need to find the drive letter. Cygwin has a way of calling my drives by a name relative to the Cygwin directory, so I get things like /home/user rather...
0
by: Derek | last post by:
I am creating an intranet using Visual Web Developer Express Edition. Everything has been working OK until yesterday when I started getting 62 messages all beginning "Could not find schema...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
1
by: amir | last post by:
Hi, When compiling a page in VS2005 this morning I received 101 messages regarding schema problems in my web.config file. When I go to view an aspx page in my IIS, IE just displays a blank...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
1
by: winston.heng | last post by:
Hi, Thanks for reading this posting. I have been cracking my head on solving the infinite loop when i call the following section code. Any help or advise is greatly appreciated =D Thanks in...
5
by: YaoBao | last post by:
Is any ColdFusion script I can put on my webpage that will create a search bar so people can type keywords to match it on the current page in my website? It will be exactly like the finder search bar...
7
by: Christian Meier | last post by:
Hello Newsgroup I have a question about the find function of std::set. When I have a "std::set<int*>", why can't I call the find() function with an "const int*"? I know that my key type is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.