mohammad shahin
mohammad shahin

Reputation: 1

How to add hyphens between elements of a string?

I have 6 different strings that I have combined together in a certain order to achieve what I want.

for a, b, c, d, e, f in zip(titles, artists, countrys, companys, values, years):
    print(a, b, c, d, e, f)

I used a zip function to print each element of each string one by one.

I just need to add a hyphen between each element.

So for example one line of my output looks like this:

Empire Burlesque Bob Dylan USA Columbia 10.9 1985

But I need this:

Empire Burlesque - Bob Dylan - USA - Columbia - 10.9 - 1985

The hyphens separate each element of the 6 strings.

I was thinking whitespace replace but that would replace too much.

Any help is appreciated ! Thank you !

Upvotes: 0

Views: 504

Answers (1)

Talon
Talon

Reputation: 1884

The print-function can do that when you supply the keyword argument sep:

for a, b, c, d, e, f in zip(titles, artists, countrys, companys, values, years):
    print(a, b, c, d, e, f, sep=" - ")

Or as an easier way for dealing with large collections: Note that your arguments should be strings then.

for description in zip(titles, artists, countrys, companys, values, years):
    print(" - ".join(description))

If it's not all string, print can handle different types and you could unpack your description:

for description in zip(titles, artists, countrys, companys, values, years):
    print(*description, sep=" - ")

Approach that allows for formatting:

for description in zip(titles, artists, countrys, companys, values, years):
    print("{} by {} from {} produced by {} going for {}$ (Year {})".format(*description))

Upvotes: 2

Related Questions