How to convert the whole element in a list which is in string to individual string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to convert the whole element in a list which is in string to individual string?

A=['hello,buddy,123'] #like this to A=['hello', 'buddy', '123']

17th Apr 2020, 7:50 AM
Jaweed
Jaweed - avatar
8 Answers
+ 5
Ginfio that's JavaScript? He is talking about python, you added python method to JavaScript or does JavaScript support split ? anyway question is in python
17th Apr 2020, 9:50 AM
Abhay
Abhay - avatar
+ 4
Ipang Buddy,well i was wondering what's the answer but U got it . Thanks for helping me
17th Apr 2020, 11:30 PM
Jaweed
Jaweed - avatar
+ 3
What happens when list <A> has more than one element or if new element added to list <A>? Is it necessary to store the splitted string into list <A>? is it okay to create new list to replace list <A>?
17th Apr 2020, 8:19 AM
Ipang
+ 3
Ipang Is it necessary to store the splitted string into list <A>? #Yes #Because, I wanted to give input in single line , A=[ ] Al=input()#ie 23,56,m A.append(Al)#when i append it takes whole element as single string so, i can't do Like this a=A[0] b=A[1] c=A[2] num= int(a)+int(b) if c=='m': print(num*1000) #one thing to say u can ask what the hell is above , if it was separate string ,we can do many things like i mentioned above is it okay to create new list to replace list <A>? # By automatically that's ok But ,if we want to do ,No
17th Apr 2020, 10:38 AM
Jaweed
Jaweed - avatar
+ 3
Jaweed0411 This function splits the string argument and does calculation as per your example. It returns zero when something isn't right. def fun(text): if len(text) == 0: return 0 text = text.split(',') if len(text) < 2: return 0 num = 0 if text[0].isdigit() and text[1].isdigit(): num = int(text[0]) + int(text[1]) if len(text) == 3 and text[2] == 'm': num *= 1000 return num
17th Apr 2020, 1:46 PM
Ipang
+ 2
Abhay o, my bad. I didn’t realize. It even says Python in the tag-_
17th Apr 2020, 4:58 PM
Ginfio
Ginfio - avatar
+ 2
@Jaweed0411 so..from reading your second comment, as I understand it...you want to split the input (a string) that uses a coma as the delimiter.. A = input().split(',') print(A)
17th Apr 2020, 5:39 PM
rodwynnejones
rodwynnejones - avatar
0
var str = "hello,buddy,123"; var newStr = str.split(","); ——————————- Test it with: console.log(newStr)
17th Apr 2020, 9:00 AM
Ginfio
Ginfio - avatar