+ 2

How to convert a list into string

S=["i"," ","p","l","a","y"] i want this into x="i play", i know we can add each list element, but can u guys tell me how to add each list element using while loop, take a look at this https://code.sololearn.com/ckusVWdc3Eq3/?ref=app

6th Jul 2020, 9:22 AM
tangella shivareddy
tangella shivareddy - avatar
25 odpowiedzi
+ 8
Most Efficient way in python: x = "".join(S)
6th Jul 2020, 9:37 AM
A C K
A C K - avatar
+ 7
You can do like this: print("".join(s)) Hope this helpful! Programming is fun!๐Ÿค—๐Ÿค—๐Ÿค—
7th Jul 2020, 11:22 PM
WenHao1223
WenHao1223 - avatar
+ 4
You could do like this(using while loop as you asked me): s=["i"," ","p","l","a","y"] i =0 x="" while i<len(s): x+=s[i] i+=1 print(x)
7th Jul 2020, 4:26 AM
Bot
Bot - avatar
6th Jul 2020, 9:32 AM
Oma Falk
Oma Falk - avatar
+ 2
x="" while s: x+=s.pop(0) print(x)
6th Jul 2020, 9:45 AM
Oma Falk
Oma Falk - avatar
+ 2
why x = "" : you cant add a character to sth. that does not exist๐Ÿ˜‰. s. pop(0) does two things: 1. it returns first entry of s 2. it deletes first entry from s for u as beginner the solution from Fา‰Rา‰Oา‰Nา‰Tา‰ ๐Ÿ”š& ๐Ÿ”™๐Ÿ”š/ ๐”ธ๐•ฃ๐••๐•ฆ๐•š๐•Ÿ๐•  Lover is the best one. Later u will find short paths.
6th Jul 2020, 9:53 AM
Oma Falk
Oma Falk - avatar
+ 2
Yes Oma Falk he is a beginner that's why he asked such questions if he would be knowing your pop function and join function then he would have done it for himself . So I tried to give him a code which dosent use any functions so I gave him this code . we just dont need to give answer we need to understand the feelings of the asker to ! ๐Ÿ˜… But who is disliking it ๐Ÿ˜…๐Ÿ˜…๐Ÿ˜…๐Ÿ˜…
6th Jul 2020, 9:56 AM
Ayush Kumar
Ayush Kumar - avatar
+ 2
i=0 x='' while i<len(s): x+=s[i] i+=1 print(x) Do this in your code and it will run successfully
7th Jul 2020, 11:53 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 2
You can also use 'for' loop like this way :- x="" for i in s: x+=i print(x)
7th Jul 2020, 11:55 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 2
Mir Abir Hossain oh yeah :D and i'm sorry i took it that way :\
7th Jul 2020, 2:37 PM
Bot
Bot - avatar
+ 1
x=โ€œ ". join(s)
6th Jul 2020, 9:28 AM
Oma Falk
Oma Falk - avatar
+ 1
Hmm use the join method to perform this task "CharByWhichToJoin".join(list) For here x="".join(S)
6th Jul 2020, 9:28 AM
Ayush Kumar
Ayush Kumar - avatar
+ 1
a=["a","y","u","s","h"] b=0 st = "" while b<=len(a)-1: st+=a[b] b=b+1 print(st) Have a look
6th Jul 2020, 9:37 AM
Ayush Kumar
Ayush Kumar - avatar
6th Jul 2020, 9:44 AM
tangella shivareddy
tangella shivareddy - avatar
+ 1
Oma Falk I am just a beginner, i dont know what pop function does, can u tell me y u have defined x with "",
6th Jul 2020, 9:49 AM
tangella shivareddy
tangella shivareddy - avatar
+ 1
Mir Abir Hossain you just copied my answer right?
7th Jul 2020, 12:17 PM
Bot
Bot - avatar
+ 1
:O :O! I just realized my code and your code is same! I didnโ€™t copy. I just tried myself. But this is s good sign. As zen of python says "There should be preferably one -- only one way to do it" I guess we both hit that way :D :D
7th Jul 2020, 12:20 PM
Mir Abir Hossain
Mir Abir Hossain - avatar
0
See my code in question, and tell me is it possible with while loop or not
6th Jul 2020, 9:31 AM
tangella shivareddy
tangella shivareddy - avatar
0
Hay tangella shivareddy why you edited your question ask the questions properly
6th Jul 2020, 9:32 AM
Ayush Kumar
Ayush Kumar - avatar