Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Converting an Integer to ASCII Characters in Python
Next article icon
Try it on GfG Practice
redirect icon

Our task is to convert a listof characters into a single string. For example, if the input is ['H', 'e', 'l', 'l', 'o'], the output should be "Hello".

Using join() 

We can convert a list of characters into a string usingjoin() method, this method concatenates the list elements (which should be string type) into one string.

Python
a=['P','y','t','h','o','n']# Convert list of characters to strings=''.join(a)print(s)

Output
Python

Explanation: ''.join(a) joins all elements in a using an empty string as the separator.

Using reduce()

reduce() function applies a function cumulatively to the elements of the list, reducing them to a single value.

Python
fromfunctoolsimportreducea=['P','y','t','h','o','n']res=reduce(lambdax,y:x+y,a)print(res)

Output
Python

Explanation:

  • lambda x, y: x + y adds two characters at a time.
  • reduce(...) repeats this process until only one string remains.

Using for Loop

If we want to join all the characters of the given list without using any inbuilt method then we can use afor loop.

Python
a=['P','y','t','h','o','n']s=''# Loop through each character in list 'a' and add it to string 's'forcina:s+=cprint(s)

Output
Python

Explanation: Iterates througha, adding each character tores and builds the final string step by step.


Convert a list of characters into a string in Python

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp