Changing type using str() and int()...how it works

2024/11/16 1:31:45

If I do this, I get:

>>> x = 1
>>> y = '2'
>>> type(x)
<class 'int'>
>>> type(y)
<class 'str'>

That all makes sense to me, except that if I convert using:

>>> str(x)
'1'
>>> type(x)
<class 'int'>
>>> int(y)
2
>>> type(y)
<class 'str'>

...why are my types only temporarily converted, i.e. why despite str(x) and int(y) is x still an integer and y is still a string?

Do I have to replace x and y to make type permanent with:

 >>> x = '1'>>> y = 2>>> type(x)<class 'str'>>>> type(y)<class 'int'>

I can see how it's useful to have the type of a variable permanently fixed, but as a new coder it's good to know what I'm contending with.

Answer

str(X) and int(X) are returning a new object/value of a given parameter. If you want to change the typ of a variable then you need to safe the new outcoming object like this:

x = 1
x = str(x)
type(x)
>>   <class 'str'>
https://en.xdnf.cn/q/120385.html

Related Q&A

Caesar cypher in Python

I am new to python and I want to write a program that asks for a line of input (each number will be separated by a single space.) It would use a simple letter-number substitution cipher. Each letter w…

Flatten a list with sublists and strings [duplicate]

This question already has answers here:Flatten an irregular (arbitrarily nested) list of lists(54 answers)How do I make a flat list out of a list of lists?(32 answers)Closed 2 years ago.How can I flat…

Guitar string code in Python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

What is the point of initializing extensions with the flask instance in Flask? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Nested list doesnt work properly

import re def get_number(element):re_number = re.match("(\d+\.?\d*)", element)if re_number:return float(re_number.group(1))else:return 1.0def getvalues(equation):elements = re.findall("…

How can I increment array with loop?

Ive got two lists:listOne = [33.325556, 59.8149016457, 51.1289412359] listTwo = [2.5929778, 1.57945488999, 8.57262235411]I use len to tell me how many items are in my list:itemsInListOne = int(len(list…

Two Complements Python (with as least bits as possible)

I am trying to output the binary representation of an negative number with the least bytes available each time.Example:-3 -> 101 -10 -> 10110

iterating through a column in dataframe and creating a list with name of the column + str

I have a dataframe with 2 coulmn, i want to iterate through the column headers, use that value and add a string to it and use it as the name of a list.rrresampled=pd.DataFrame() resampled[RAT]=dd1[RAT]…

Python multiple inheritance name clashes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

How to read multiple txt file from a single folder in Python? [duplicate]

This question already has answers here:How to open every file in a folder(8 answers)Closed 3 years ago.How can I read multiple txt file from a single folder in Python?I tried with the following code b…