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

Add Characters Between Each Character

Insert a separator between string characters with join. Students treat a string as an iterable of characters and avoid adding separators at either endpoint.

IntermediatePhase 5 - Category 4: String TransformationExample 8 of 10
add-characters-between-each-character.py
1# Take string input
2s = input("Enter a string: ")
3char = input("Enter character to add: ")
4
5# Add between characters
6result = char.join(s)
7print(f"Result: {result}")

Output

Enter a string: Hello
Enter character to add: -
Result: H-e-l-l-o

In this one

  • Use a separator as receiver
  • Join an iterable of characters
  • Avoid endpoint separators

How it works

  1. 1

    Read the source and separator.

  2. 2

    Call char.join(s).

  3. 3

    Print the joined character sequence.

Edge cases

What happens for one character?

What happens for one character?

It is returned without a separator.

What happens for empty s?

What happens for empty s?

The result is empty.

What's going on

char.join(s) places the separator between consecutive characters. The separator is the method's receiver; the text to join is its argument.

For abc and separator -, the result is a-b-c. No extra separator appears before a or after c. The separator response can itself contain multiple characters. Empty and one-character strings naturally contain no character gaps to fill.

What happens for one character? It is returned without a separator.

What happens for empty s? The result is empty.

Quiz

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

Question 1

Where does join insert separators?

Pick an answer to check it.

Question 2

What happens for one character?

Pick an answer to check it.

Questions people ask

What happens for one character?

It is returned without a separator.

What happens for empty s?

The result is empty.

Where does join insert separators?

Between elements only.