+ 1
I have a question.
PYTHON If i make a list like ranks = [“jack”, “king”, “9”] #and then do this ranks[0] = 10 ranks[1]=8 ranks[2]=9 I know i have stored them as integers but i don’t want them to be printed as integers i want them to be printed as strings but still have the value of the integer is it possible?
5 Respuestas
+ 6
print (str(ranks[0]))
This will print the value as a string without changing its type. Why?
+ 1
use a dictionary...
rank={"jack" : 10, "king" : 8, ...}
havd a look at the python đict. tutorial...
rank["jack"]=10;...
+ 1
Sayan thank you very much i will try it out.
0
Maxim i have tried that but then it us already stored as strings and even if i were to use int it will be print it as and integer
0
@Abass_J1 I still cannot get your problem.
>>> ranges = [1,2,3]
>>> type(str(ranges[0]))
<class 'str'>
>>> type(ranges[0])
<class 'int'>
Type of the variable won't change unless you make it explicitly, like
ranges[0] = str(ranges[0])
or apply type coercion.
Post a code that causes your question to clarify the problem.