Search code examples
loopsjoinreplacerange

Python - .join() OR .replace() at the start of a loop with range?


Updated Using Python Question Summarized: Is it possible to add a .replace() OR .join() in a loop? Attempting to NOT have to add .replace/.join to every print statement???

Input:

for a in range(1, 10):
    print("Squirtle",a)
    print("Bulbasaur",a)
    print("Charmander",a)

Output:

Squirtle 1
Bulbasaur 1
Charmander 1
Squirtle 2
etc.
etc.
etc.

My Goal is to replace the whitespace " " or join the values without having to join/replace in every print. statement. Is that possible? See below:

Squirtle1
Bulbasaur1
Charmander1
Squirtle2

Solution

  • Either you're thinking too complicated or I'm misunderstanding the question. I guess you could just add the two together and get the result you want?

    for a in range(1, 10):
        a = str(a)
        print("Squirtle" + a)
        print("Bulbasaur" + a)
        print("Charmander" + a)
    

    Output:

    Squirtle1
    Bulbasaur1
    Charmander1
    Squirtle2
    Bulbasaur2
    Charmander2
    etc...