Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Convert Array to String

Join integer list values into one Python string. Students convert each value before join and understand why a separator-free representation can lose boundaries.

IntermediatePhase 6 - Category 2: Array & String CombinationsExample 1 of 10
convert-array-to-string.py
1# Take array
2n = int(input("Enter array size: "))
3arr = []
4for i in range(n):
5 arr.append(int(input(f"Element {i+1}: ")))
6
7# Convert to string
8result = "".join(str(x) for x in arr)
9print(f"String: {result}")

Output

Enter array size: 5
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Element 5: 5
String: 12345

In this one

  • Convert values before joining
  • Choose an empty separator
  • Recognize lost element boundaries

How it works

  1. 1

    Collect the integer list.

  2. 2

    Generate str(x) for every element.

  3. 3

    Join with an empty separator and print String.

Edge cases

What if arr is empty?

What if arr is empty?

The joined result is empty.

Why use str(x)?

Why use str(x)?

join cannot directly concatenate integer elements.

What's going on

join requires strings, so str(x) converts each integer before the empty separator joins the pieces. The list's values are not arithmetically added.

For [12, 3], the result is 123, the same text produced by [1, 23]. No separator preserves element boundaries, so this is not a reversible serialization format. Negative signs remain part of each converted integer's text.

What if arr is empty? The joined result is empty.

Why use str(x)? join cannot directly concatenate integer elements.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Can the original boundaries always be recovered?

Pick an answer to check it.

Question 2

What if arr is empty?

Pick an answer to check it.

Questions people ask

What if arr is empty?

The joined result is empty.

Why use str(x)?

join cannot directly concatenate integer elements.

Can the original boundaries always be recovered?

No.