1

Trying to produce this as output 1H, 2i, 3p, 4p, 5o, 6p, 7o, 8t, 9a, 10m, 11u, 12s,

but keep receiving 1 H, 2 i, 3 p, 4 p, 5 o, 6 p, 7 o, 8 t, 9 a, 10 m, 11 u, 12 s,

animal = "Hippopotamus"

for i, letter in enumerate(animal):
    print(i + 1, letter, end = ", ")

1 H, 2 i, 3 p, 4 p, 5 o, 6 p, 7 o, 8 t, 9 a, 10 m, 11 u, 12 s,
m0ng00se
  • 69
  • 8
  • 1
    Side-note: You can save yourself some math by changing it to `enumerate(animal, start=1)` (`start=` is optional, `enumerate(animal, 1)` also works). Then just `print` `i`, not `i + 1`. – ShadowRanger Aug 30 '20 at 23:52

3 Answers3

4

I modified the print function a little like this;

animal = "Hippopotamus"

for i, letter in enumerate(animal):
    print(f"{i + 1}{letter}", end = ", " )

Or you can use another option like this;

for i, letter in enumerate(animal):
    print("{}{}".format(i + 1, letter), end = ", " )

Or you can use sep='' in print function.

for i, letter in enumerate(animal):
    print(i + 1, letter, end=", ", sep='')
Ahmed Mamdouh
  • 696
  • 5
  • 12
  • This works, but it's basically bypassing `print` functionality in favor of generating additional temporary strings. `print` does support removing the implicit space separator with `sep=''`. – ShadowRanger Aug 30 '20 at 23:51
  • Thank you for providing the other options. Adding `sep=''` requires least amount of changes to script. – m0ng00se Aug 31 '20 at 16:01
4

Add sep='' to print's arguments.

superb rain
  • 5,300
  • 2
  • 11
  • 25
0

animal = "Hippopotamus"

for i, letter in enumerate(animal):

l = f"{i+1}{letter}"





print(l, end = ", " )

Use the f"" string.