Jump to content

Python - Integer to String


markinglevfx

Recommended Posts

In the Python SOP, I've run into a wall trying to get an integer converted to a string. In the larger picture, I'm trying to subtract 1 from a value which is currently part of a string. There are probably neater ways to write these steps, but I'm fairly new to Python so I'm writing it all out to make things easier to follow.

I start with a string such as: 

string = "f27"

Then remove the 'f' like so:

string = string[1:]

Convert it to an integer:

integer = int(string)

Subtract 1 from the value:

integer -= 1

set new string

string_new = "f" + str(integer)

 

The last line returns an error on the Python SOP: TypeError: str() object is not callable. This is confusing because these same steps work just fine in the Python shell. Is there an alternative to str(), or am I using it wrong?

Link to comment
Share on other sites

I did have 'str' set as a variable elsewhere in the code. But even after I got rid of it, the error persisted.

 

But I have managed to get it working now. I solved it by making a new Python SOP and using the same code - initially I was copy/pasting lines and seeing where it broke. Weird how I now have two python nodes side by side with the same inputs, the old one errors and the new one is fine.

Link to comment
Share on other sites

  • 2 weeks later...

Don't know if this is a thing in python but I guess it might be worth mentioning, its probably a good idea to refrain from naming your variables stuff like string, int/integer, float, vector etc because this be messing with the internal classes and structs of your coding language and give you hassle later. Don't know if you just named them that in your example for demonstration purposes or not but figured I'd add my 2 cents.

Link to comment
Share on other sites

  • 1 month later...

TypeError: 'str' object is not callable usually means you are using your () notation on a string, and Python tries to use that str object as a function. e.g. "hello world"(), or "hello"("world"). Or,  if you have variable str and trying to call str() function. Finally: Do not declare python variable name same with python built-in function name, keyword etc.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...