Python: Convert List to String (join & for loop methods explained)

Hello, this is BlockDMask.

Today, we will learn how to convert a Python list into a string.

We will cover:

  • Lists with only strings
  • Lists containing both strings and numbers
  • How to use both join() and for loop methods

Table of Contents



1. Convert list to string using join()

The easiest way to convert a list of strings into a single string is by using Python’s join() method.

1-1) Simple join()

fruits = ['apple', 'banana', 'cherry', 'date', 'BlockDMask']

result = ''.join(fruits)
print(result)

Output:

applebananacherrydateBlockDMask

1-2) Add separator while joining

fruits = ['apple', 'banana', 'cherry', 'date', 'BlockDMask']

result_comma = ', '.join(fruits)
print(result_comma)

result_dash = ' - '.join(fruits)
print(result_dash)

result_newline = '\n'.join(fruits)
print(result_newline)

Output:

apple, banana, cherry, date, BlockDMask
apple - banana - cherry - date - BlockDMask
apple
banana
cherry
date
BlockDMask

1-3) If list contains numbers

You need to convert non-string elements into strings before using join().

items = [100, 200, "BlockDMask", 300, 400]

result = ''.join(str(item) for item in items)
print(result)

result_comma = ', '.join(str(item) for item in items)
print(result_comma)

Output:

100200BlockDMask300400
100, 200, BlockDMask, 300, 400


2. Convert list to string using for loop

When you need more control, you can use for loop to manually build the string.

2-1) For list of only strings

languages = ['Python', 'Java', 'C++', 'Go', 'Rust']

result = ''
for lang in languages:
    result += lang

print(result)

Output:

PythonJavaC++GoRust

2-2) For list with numbers (using function)

def list_to_string(lst):
    result = ''
    for element in lst:
        result += str(element)
    return result

data = [10, 20, "BlockDMask", 30, 40]
result = list_to_string(data)
print(result)

Output:

1020BlockDMask3040

2-3) Custom separator using for loop (skip separator for last element)

def list_to_string(lst, sep):
    result = ''
    for index, element in enumerate(lst):
        if index == len(lst) - 1:
            result += str(element)
        else:
            result += str(element) + sep
    return result

data = [10, 20, "BlockDMask", 30, 40]

print(list_to_string(data, ''))
print(list_to_string(data, ', '))
print(list_to_string(data, '/'))
print(list_to_string(data, '\n'))

Output:

1020BlockDMask3040
10, 20, BlockDMask, 30, 40
10/20/BlockDMask/30/40
10
20
BlockDMask
30
40

2-4) Add prefix/suffix for each item (more customized)

def format_list(lst, prefix='', suffix='', sep=', '):
    result = ''
    for index, element in enumerate(lst):
        formatted = f"{prefix}{element}{suffix}"
        if index == len(lst) - 1:
            result += formatted
        else:
            result += formatted + sep
    return result

data = ['apple', 'banana', 'cherry', 'date']

print(format_list(data, prefix='[', suffix=']', sep=' | '))

Output:

[apple] | [banana] | [cherry] | [date]

→ Now you can fully customize how each list item is formatted into a string.



In this post, we have learned how to convert lists into strings in Python using both join() and for loop methods, including custom separators and formats.
Thanks for reading!

Comments

Popular posts from this blog

Python split() Function: Usage and Examples

Privacy Policy for Android Apps

How to Write Comments in Python (Single-line, Multi-line, Shortcuts & Indentation Tips)