• Register
1 vote
48 views

Problem:

I am beginner in pyhton prgramming language, I tried to convert string array to int array using python so I wrote a code which contains-
testing_list = ['4', '2', '3', '9', '5'] 

And my code is:

testing_list = ['4', '2', '3', '9', '5'] 

print ("Main list is : " + str(testing_list)) 

for i in range(0, len(testing_list)): 
    testing_list[i] = int(testing_list[i]) 
    print ("Converted list is : " + 'str(testing_list)') 

And my codes output is-

Main list is : ['4', '2', '3', '9', '5']
Converted list is : str(testing_list)
Converted list is : str(testing_list)
Converted list is : str(testing_list)
Converted list is : str(testing_list)
Converted list is : str(testing_list)

why am I not getting the converted result and what is the way to get the acurate result?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

You should not put your string in a qoute. You should have written the code as-
testing_list = ['4', '2', '3', '9', '5'] 

print ("Main list is : " + str(testing_list)) 

for i in range(0, len(testing_list)): 
    testing_list[i] = int(testing_list[i]) 
   
    print ("Converted list is : " + str(testing_list)) 

And the output is:

Main list is : ['4', '2', '3', '9', '5']
Converted list is : [4, '2', '3', '9', '5']
Converted list is : [4, 2, '3', '9', '5']
Converted list is : [4, 2, 3, '9', '5']
Converted list is : [4, 2, 3, 9, '5']
Converted list is : [4, 2, 3, 9, 5]

Your problem was in the line-

 print ("Converted list is : " + 'str(testing_list)') 

 

11 6 4
34,950 points

Related questions

0 votes
1 answer 33 views
33 views asked Oct 23, 2020 Shaad Sh 800 points
0 votes
1 answer 4 views
4 views
Problem: how to I Solve this: convert string array to int array python HELP! I need help understanding this: convert string array to int array python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 9 Mashhoodch 13k points
0 votes
1 answer 32 views
32 views
Problem: Hi there! I am trying to convert an integer number to a byte array using following snippet: static void Main(string[] args) { int i = 44; byte[] b = Convert.ToByte(i); //Error foreach (byte byt in b) Console.WriteLine(byt); Console.ReadKey(); ... says “Cannot implicitly convert byte to byte[]”. Does anybody have any idea why this error is occuring and how can I resolve it?
asked Nov 18, 2020 Code Learner 9.2k points
0 votes
1 answer 4 views
4 views
Problem Someone can help me how i convert char array to int array in c++
asked Mar 27 Subhendu 4.1k points
1 vote
1 answer 694 views
694 views
Problem: I am getting the following error : error: array initializer must be an initializer list or string literal For the array; char x[80] = GetString(); Can any body explain or help?
asked Apr 2, 2020 LizzyM 6.1k points
0 votes
1 answer 131 views
0 votes
1 answer 4 views
4 views
Problem: Need help with the conversion of all strings to integers. Convert int list to string list python.
asked Mar 23 PkGuy 13.1k points
0 votes
1 answer 33 views
33 views
Problem: I've got some Python code that runs through a list of strings and converts them to integers or floating point numbers if possible. Doing this for integers is pretty easy if element.isdigit(): newelement = int(element) Floating point numbers are more difficult. Right now I'm using partition('.') to split the string and checking to make sure that one or both sides are digits.
asked Feb 21 Mashhoodch 13k points
1 vote
1 answer 24 views
24 views
Problem: I was tring to find the length of a string in python. So I wrote a code: string = 0 for c in "popular": string+==1 print (string) which gives following error; File "ab.py", line 3 string+==1 ^ SyntaxError: invalid syntax Is this the right way to do this or there are other waysto find this out?
asked Mar 28, 2020 LizzyM 6.1k points
1 vote
1 answer 96 views
96 views
Problem: I am a python newbee. I was trying to print string and int on same line of a python code. The code I wrote is: a = int(input("Please Enter a Number: ")) print("Your Number is" a) but when I run this it shows an error message: File "main.py", line 2 print("Your Number is" x) ^ SyntaxError: invalid syntax I am not able to do this please help me to solve this problem?
asked Mar 27, 2020 LizzyM 6.1k points