I have a question. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

7th Mar 2018, 10:27 PM
Abass_J1
Abass_J1 - avatar
5 Answers
+ 6
print (str(ranks[0])) This will print the value as a string without changing its type. Why?
7th Mar 2018, 11:34 PM
strawdog
strawdog - avatar
+ 1
use a dictionary... rank={"jack" : 10, "king" : 8, ...} havd a look at the python đict. tutorial... rank["jack"]=10;...
8th Mar 2018, 4:17 AM
sayan chandra
sayan chandra - avatar
+ 1
Sayan thank you very much i will try it out.
8th Mar 2018, 6:12 AM
Abass_J1
Abass_J1 - avatar
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
8th Mar 2018, 6:11 AM
Abass_J1
Abass_J1 - avatar
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.
8th Mar 2018, 11:18 AM
strawdog
strawdog - avatar