M
M
Mr.nobody2018-09-14 12:04:36
Python
Mr.nobody, 2018-09-14 12:04:36

How to use sep= in Python?

Hello. Task: there is a list ["Foo_1", "Foo_2", "Foo_3" ]
output to the console the elements of the list in the form Foo_1, Foo_2, Foo_3
note the comma after the first and second element and its absence after the third.
tried like this:

spisok = [1, 2, '3']
for x in spisok:
    print(x, sep=", ")  
output:
1
2
3

so:
spisok = [1, 2, '3']
for x in spisok:
    print(x, end=" ", sep=", ")
output:
1 2 3

and other options.
I understand sep= only works in this form
print('1', '2', '3', sep=", ")
output:
1, 2, 3

I searched in Google, I didn’t find it (maybe I searched badly)
Tell me how to solve the problem, maybe there are other options besides sep =.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aRegius, 2018-09-14
@no_one

print(*your_list, sep=', ')

I
iikoreva, 2022-01-09
@iikoreva

Task: write a function that will return the concatenated version of the words separated by hyphens from a list.
First solution (not mine - suggested by Senior after reviewing mine):

def words(*args):
    print(*args, sep='-')

words("i", "love", "python", "!")

The second is mine, dreary and big, but my own :)))
def words(*words):
    expr = []
    print(len(words))
    rep = (len(words))
    for i in range(rep):
        if i < rep-1:
            expr.append(words[i])
            expr.append('-')
        else:
            expr.append(words[i])
    expression = "".join(expr)
    print(expression)
words("i", "love", "python", "!")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question