Reinititalize 'str' as a type [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have used str as a variable. Now, I would like to convert an int into a string. For this, normally I would use str(10). What should I do in this case?

You can just delete the variable to get str() back:
othervarname = str
del str

Find and replace "str" with "sensibleNameForYourVariable", then use str(i) to convert integers to strings.

Use the code below if you had used str as a variable. This will solve your problem. The keyword str() internally uses __str__() function so better use that function only
str = 1
str.__str__()
Output
'1'
As in your case you should use.

Retrieve it from __builtins__.
str = "shadowed"
assert __builtins__.str(3) == "3"

Related

new to python having trouble printing 4 variables [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
adding = int(5+3)
subtract = int(10-2)
multiplication = int(2*4)
division = int(16/2)
print (str(adding,"\n",subtract,"\n",multiplication,"\n",division))
im getting a typeError: TypeError: str() takes at most 3 arguments (7 given)
Solution:
Just change this piece of code from this
print (str(adding,"\n",subtract,"\n",multiplication,"\n",division))
To this.
print (adding,"\n",subtract,"\n",multiplication,"\n",division)
Reason: str() is a typecast method. You don't need to typecast each and everything.
For Reference:
https://www.geeksforgeeks.org/type-casting-in-python-implicit-and-explicit-with-examples/
Think of str as a function that takes in a variable and converts it into a string. Passing multiple variables to str() separated by comma will not apply string function to each variable. It will instead mean that they are arguments to the function.
If you want to convert each variable into
adding = str(int(5+3))
subtract = str(int(10-2))
multiplication = str(int(2*4))
division = str(int(16/2))
print (adding,"\n",subtract,"\n",multiplication,"\n",division)
Note that print function accepts multiple arguments for printing purposes.
To convert an integer to string, you need to use str() each variables separately.
Also, you can use + as concat operator to avoid extra space.
print(str(adding)+"\n"+str(subtract)+"\n"+str(multiplication)+"\n"+str(division))

Sum of the digits [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to get the sum of the digits of an integer, and found the solutions below:
sum(map(int,list(str(123))))
sum(int(digit) for digit in str(123))
However, when I run this, I get the below error:
TypeError: 'int' object is not callable
Contrary to other answers, both your code works fine.
I think you are shadowing something on your code. Perhaps you used int as a variable?
sum() works on iterables.
int(digit) for digit in str(123)
This returns an generator, and should work, as said by other answers, take a look at this answer.
The below should also do the job:
sum([int(digit) for digit in '123'])
Hope this helps!
sum(int(digit) for digit in str(123))
The above code should work.
However you said you get the error,
TypeError: 'int' object is not callable
That error suggests that you're using type int instead of a str
.
Did you use another variable for that?
For example, the below code should give you that error you mentioned
obj = 123
sum(int(digit) for digit in obj)
You have to ensure that obj is of a str type.
sum() works in iterable objects. You need to create a list with the digits you are trying to add. The code below should do that:
sum([int(x) for x in str(123)])

Error while trying to convert string into a dictionary (python) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to convert the following into a dictionary
my_string='{"values": [["searchText", "aaaa bbbb"], ["url", "https://aaa.com/w/Try.do?one=yes&isFate=true&active=2&entry=aaaa+bbbb"], ["ip", "11.22.333.44, 12.12.3.111"], ["userId", "4569874"], ["id", 69875462145], ["sessionId", "25F8D032D8FGJ5ED023F56ZZ.TRYING207"], ["pageNumber", null], ["dateCreated", "2018-03-06 17:23:55"], ["page_id", 771790], ["dateModified", "2018-03-06 17:23:55"], ["device_id", 168]]}'
But when I do this:
ast.literal_eval(my_string)
It raises the following error:
ValueError: malformed node or string: <_ast.Name object at 0x000001F2C0536080>
Can you help me with this please ?
Thank you in advance
PS: I'm using python 3.6
The easiest way to load your data is to use json.loads function as was mentioned in comment to your question.
But if you strongly want to use ast.literal_eval function, the reason of ValueError is this part of my_string:
... ["pageNumber", null] ...
There is no null constant, class or type in Python. Change it to None.

changing variable type in list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to take 4 numbers as string and split them into 4 elements. and convert each of them into integer and save it in a list. What am I doing wrong here?
mask = "250.250.0.0"
string = mask.split(".")
toInt = [int[i] for i in string]
print(toInt)
error message says type object is not subscriptable
use int(i) instead of int[i], int is not subscriptable
int is a builtin-class that provides no implementation for subscription in array-like fashion.
To convert to an integer you should use int like this:
toInt = [int(i) for i in string]

Python - TypeError: string indices must be integers [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
For some reason this piece of script is returning the error: "TypeError: string indices must be integers"; and I cannot see what's wrong. Am I being stupid and overlooking an obvious mistake here? I can't see one for the life of me!
terms = {"ALU":"Arithmetic Logic Unit"}
term = input("Type in a term you wish to see: ")
if term in terms:
definition = term[terms]
sentence = term + " - " + definition
print(sentence)
else:
print("Term doesn't exist.")
I think you want it this way: definition = terms[term]
This line definition = term[terms] is trying to get a character out of the string term. You probably just typoed, and want
definition = terms[term]
^ here, reference the dict, not the string
You are indexing the string term instead of the dictionary terms. Try:
definition = terms[term]
You accidentally swapped the variables. Change this:
definition = term[terms]
To this:
definition = terms[term]

Categories

Resources