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']
8 Answers
+ 4
Ipang
Buddy,well i was wondering what's the answer but U got it .
Thanks for helping me
+ 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>?
+ 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
+ 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
+ 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)
0
var str = "hello,buddy,123";
var newStr = str.split(",");
ââââââââââ-
Test it with:
console.log(newStr)