473,326 Members | 2,128 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,326 software developers and data experts.

Do I need to convert string to integer in python?

Do I need to convert string to integer in python? or it will do it for
me (since dynamic type)?

In my python script, I have this line:
x /= 10;

when i run it, I get this error:
TypeError: unsupported operand type(s) for /=: 'unicode' and 'int'

I want to divide x by 10 and assign that value back to x.

Thank you.

Feb 26 '06 #1
5 3403
Al************@gmail.com wrote:
Do I need to convert string to integer in python? or it will do it for
me (since dynamic type)?


Yes. Dynamic typing doesn't say anything about a string and a number being
"equal," as they are (e.g.) in Perl, it just says that you don't have to
care what type of object a name is bound to. What you are thinking of is
called weak typing (auto-coercion between strings and numbers and such).

You'd have to do something like:
x = int(x)/10

--- Heiko.
Feb 26 '06 #2
<Al************@gmail.com> wrote:
Do I need to convert string to integer in python? or it will do it for
me (since dynamic type)?
Nope, no such implicit conversion (thanks be!). Strings are strings and
ints and ints and never the twain shall meet, except by explicit
request;-).

In my python script, I have this line:
x /= 10;

when i run it, I get this error:
TypeError: unsupported operand type(s) for /=: 'unicode' and 'int'

I want to divide x by 10 and assign that value back to x.


If you want x to remain a Unicode string when you're done,

x = unicode(int(x) / 10)

should work. If you want x to become an integer, omit the unicode call
around the int(x)/10 expression.
Alex
Feb 26 '06 #3
Al************@gmail.com schrieb:
Do I need to convert string to integer in python? or it will do it for
me (since dynamic type)?

In my python script, I have this line:
x /= 10;

when i run it, I get this error:
TypeError: unsupported operand type(s) for /=: 'unicode' and 'int'

I want to divide x by 10 and assign that value back to x.


Yes, you have to convert beforehand. Dynamic typing and weak typing (as in perl or php) aren't the same thing.

Dynamic typing means that you can rebind a variable (or better name) at runtime to contain a value with a new type. Weak
typing means that an object is reinterpreted as a value of another type when needed - and (often) leads to difficult errors.
Regards,

Diez
Feb 26 '06 #4
Al************@gmail.com schrieb:
Do I need to convert string to integer in python? or it will do it for
me (since dynamic type)?

In my python script, I have this line:
x /= 10;

when i run it, I get this error:
TypeError: unsupported operand type(s) for /=: 'unicode' and 'int'

I want to divide x by 10 and assign that value back to x.

Thank you.
x=100
x/=10
x 10 x='100'
x/=10
Traceback (most recent call last):
File "<pyshell#4>", line 1, in -toplevel-
x/=10
TypeError: unsupported operand type(s) for /=: 'str' and 'int' x=int(x)/10
x

10

Michael
Feb 26 '06 #5
On Sun, 26 Feb 2006 11:55:54 -0800, Allerdyce.John wrote:
Do I need to convert string to integer in python? or it will do it for
me (since dynamic type)?

In my python script, I have this line:
x /= 10;

when i run it, I get this error:
TypeError: unsupported operand type(s) for /=: 'unicode' and 'int'


No, the Python interpreter is playing a practical joke on you when it
raises that exception. Just persevere with the x /= 10 line, and
eventually the interpreter will give in and give you the result you need.
But be warned, if it is in a particularly playful mood, you may need to
try hundreds of times before it will stop messing about and get back to
work.

By the way, semi-colons are not required at the end of lines in Python.
--
Steven.

Feb 26 '06 #6

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

Similar topics

3
by: Graham Nicholls | last post by:
Hi, I'm trying to size a jpeg file. The file size is held in a short (2 byte integer) at a certain offset. Once I've found these two bytes (they're in MSB,LSB order), I need to convert them to...
7
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the...
21
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
2
by: phillip.s.powell | last post by:
I have to migrate data from one database table to another table in another database where the fields do not match, not even in the same order, and even if they do match, on occasions the datatypes...
27
by: fdu.xiaojf | last post by:
Hi, String formatting can be used to converting an integer to its octal or hexadecimal form: '307' 'c7' But, can string formatting be used to convert an integer to its binary form ?
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
8
by: te509 | last post by:
Hi guys, does anybody know how to convert a long sequence of bits to a bit-string? I want to avoid this: '949456129574336313917039111707606368434510426593532217946399871489' I would...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.