How to convert a list into string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answers
+ 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
6th Jul 2020, 9:40 AM
tangella shivareddy
tangella shivareddy - 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
+ 1
F҉R҉O҉N҉T҉ 🔚& 🔙🔚/ 𝔸𝕣𝕕𝕦𝕚𝕟𝕠 Lover Can u tell me, why u have initialised st with ("")
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