How to convert a list into string | SoloLearn: Learn to code for FREE!

+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

7/6/2020 9:22:18 AM

tangella shivareddy

26 Answers

New Answer

+9

tangella shivareddy You can do like this also 👇 a=["K","i","i","b","o"," ","G","h","a","y","a","l"] print(*a,sep="") output : Kiibo Ghayal

+7

Most Efficient way in python: x = "".join(S)

+6

You can do like this: print("".join(s)) Hope this helpful! Programming is fun!🤗🤗🤗

+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)

+3

F҉R҉O҉N҉T҉ 🔚& 🔙🔚/ 𝔸𝕣𝕕𝕦𝕚𝕟𝕠 Lover Thank you, that's what i am looking for

+2

https://code.sololearn.com/c3JbqP4CEE0w/?ref=app

+2

x="" while s: x+=s.pop(0) print(x)

+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.

+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 😅😅😅😅

+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

+2

You can also use 'for' loop like this way :- x="" for i in s: x+=i print(x)

+2

Mir Abir Hossain oh yeah :D and i'm sorry i took it that way :\

+1

x=“ ". join(s)

+1

Hmm use the join method to perform this task "CharByWhichToJoin".join(list) For here x="".join(S)

+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

+1

F҉R҉O҉N҉T҉ 🔚& 🔙🔚/ 𝔸𝕣𝕕𝕦𝕚𝕟𝕠 Lover Can u tell me, why u have initialised st with ("")

+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 "",

+1

Mir Abir Hossain you just copied my answer right?

+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

0

See my code in question, and tell me is it possible with while loop or not