Search code examples
pythonlistformatadditionitems

How to add "h" list's items(elements)?


I want to add "h" (meaning hours) to list's items/elements? Couldn't find nice and easy way to do it.

paivat = int(input("xxxxxxxx: "))
tyo = list()

# Codes...

tyo.append(tunnit)

print("Syötit seuraavat tunnit:", *tyo, "yhteensä", total, "tuntia.", sep = " ")

gives:

Syötit seuraavat tunnit: 5.0 4.0 yhteensä 9.0 tuntia.

I would like print it like:

Syötit seuraavat tunnit: 5.0**h** 4.0**h** yhteensä 9.0 tuntia.

Could you assist me with this?


Solution

  • Here's one way with f string literals:

    print(f"Syötit seuraavat tunnit: { ' '.join(map(lambda t: f'{str(t)}**h**', tyo)) } yhteensä { total } tuntia.")