Hello
How can I find out in Python whether the operand is integer or a
character and change from char to int ?
Regards,
L. 9 1759
>>> thisisastring = "1" thisisanint = 1 type(thisisastr ing)
<type 'str'> type(thisisanin t)
<type 'int'> thisisastring = int(thisisastri ng) thisisanint = str(thisisanint ) type(thisisastr ing)
<type 'int'> type(thisisanin t)
<type 'str'>
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
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/ ro***********@g mail.com wrote: thisisastri ng = "1" thisisani nt = 1 type(thisis astring) <type 'str'> type(thisis anint) <type 'int'> thisisastri ng = int(thisisastri ng) thisisani nt = str(thisisanint ) type(thisis astring) <type 'int'> type(thisis anint) <type 'str'> print repr(thisisastr ing)
1 print repr(thisisanin t)
'1' thisisastring = 'a' thisisanint = 98 thisisastring = ord(thisisastri ng) #Using ASCII rep. thisisanint = chr(thisisanint ) type(thisisastr ing)
<type 'int'> type(thisisanin t)
<type 'str'> print repr(thisisastr ing)
97 print repr(thisisanin t)
'b'
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
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
-----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>
iEYEARECAAYFAkO ZwtkACgkQYu1fMm OQldXEzACgqdDVv x29UBVSIfQWnGRi AAk9
xPsAn0yN5jWrUN+ 6SKIHdwtILRBVyQ wR
=HZQq
-----END PGP SIGNATURE-----
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***********@a cm.org
Thank you ALL for help and explanation
Regards,
L. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 than
/cygdrive/g/cygwin/home/usr, etc. How can I find the letter of the
drive, or in the above example, the letter 'g'?
My program needs to run on an external media that comes with Cygwin on
it. I have no control over what drive is assigned to...
|
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 information for the".
I am using Cassini as the web server on my PCand I can still run my site
from within VWD. Does anyone know what I have done to cause these messages to
appear?
Could not find schema information for the element...
|
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 want to replace all of them.
You need to look at it in a case-by-case basis. What can you do?
Answer: emacs.
|
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
page.
I have the June Atlas CTP installed.
|
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
System.CodeDom.CodeCastExpression)
__problem typecast #1
Desc:i do needed checks but data/commands in XML is dynamic and i don't
wanna fix C# code again and again...
Sample:foreach (object some in somearray)
(some.GetType())some.someaction();
| |
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 advance.
Cheers!
|
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 that comes with your browser window, IE; click edit, then find on this page, which brings up the search box.
I know that JavaScript can do this. I tried to insert JavaScript to my .cfm page. It doesn't work. Any help will be appreciated.
Here...
|
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 different from the type of the
parameter I give to the find function but can't the find() function be
written in a way where this would work? Normally, "int*" can be compared
with "const int*" without problems...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |