Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program To Convert An Array List Into A String And Viceversa
Python provides several methods to convert between lists and strings. The most common approaches are using join() to convert lists to strings and split() to convert strings back to lists.
Converting a List to String Using Loop
You can iterate through all items in a list and concatenate them into a string ?
words = ["I", "want", "cheese", "cake"]
result = ""
for item in words:
result = result + item + " "
print(result.strip()) # Remove trailing space
I want cheese cake
Using join() Function
The join() function is the most efficient way to convert a list into a string. It joins all items of an iterable using a specified separator. All elements in the list should be strings.
Syntax
separator.join(iterable)
Where the separator is the character(s) used to join the elements.
Example with Different Separators
words = ["I", "want", "cheese", "cake"]
print("Original list:", words)
str1 = " ".join(words)
str2 = "*".join(words)
str3 = "@".join(words)
print("Space separator:", str1)
print("Star separator:", str2)
print("At separator:", str3)
Original list: ['I', 'want', 'cheese', 'cake'] Space separator: I want cheese cake Star separator: I*want*cheese*cake At separator: I@want@cheese@cake
Handling Mixed Data Types
If the list contains non-string elements, convert them to strings first using str() ?
mixed_list = ["I", 2, 3, "want", "cheese", "cake"]
print("Original list:", mixed_list)
result = " ".join(str(item) for item in mixed_list)
print("Converted string:", result)
Original list: ['I', 2, 3, 'want', 'cheese', 'cake'] Converted string: I 2 3 want cheese cake
Converting String to List Using split()
The split() function converts a string into a list by splitting it based on a separator. If no separator is specified, whitespace is used by default.
Syntax
string.split(separator)
Example without Separator
When no separator is specified, the string is split on whitespace ?
sentence = "let the matriarchy begin"
print("Original string:", sentence)
word_list = sentence.split()
print("Resulting list:", word_list)
Original string: let the matriarchy begin Resulting list: ['let', 'the', 'matriarchy', 'begin']
Using Comma Separator
dishes = "My favourite dishes are Dosa, Burgers, ice creams, waffles"
print("Original string:", dishes)
dish_list = dishes.split(",")
print("Resulting list:", dish_list)
Original string: My favourite dishes are Dosa, Burgers, ice creams, waffles Resulting list: ['My favourite dishes are Dosa', ' Burgers', ' ice creams', ' waffles']
Using Custom Separator
email = "chrisevans@gmail.com"
print("Original string:", email)
parts = email.split("@")
print("Resulting list:", parts)
Original string: chrisevans@gmail.com Resulting list: ['chrisevans', 'gmail.com']
Comparison of Methods
| Operation | Method | Best For | Performance |
|---|---|---|---|
| List to String | join() |
Most cases | Fast |
| List to String | Loop + concatenation | Learning purposes | Slow |
| String to List | split() |
All cases | Fast |
Conclusion
Use join() for converting lists to strings as it's the most efficient method. Use split() to convert strings back to lists with customizable separators. Both methods are essential for string manipulation in Python.
