6
pets = ['boa', 'cat', 'dog']
for pet in pets:
    print(pet)

boa
cat
dog
>>> for pet in pets:
        print(pet, end=', ')

boa, cat, dog, 
>>> for pet in pets:
        print(pet, end='!!! ')

boa!!! cat!!! dog!!! 

but what about sep? i tried to replace end by sep but nothing happened but i know that sep is used to separete while printing, how and when can i use sep? what are the differences between sep and end?

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
MMM
  • 521
  • 2
  • 5
  • 10
  • 3
    `sep` is used to separate arguments passed as `*args`. Try running `print('a', 'b', 'c', sep=', ')` – vaultah Apr 09 '16 at 05:17
  • 1
    yea i got it, thanks, its all that sep wasn't working because i placed it in a for loop... only end works in for loop.. – MMM Apr 09 '16 at 20:17

4 Answers4

12

The print function uses sep to separate the arguments, and end after the last argument. Your example was confusing because you only gave it one argument. This example might be clearer:

>>> print('boa', 'cat', 'dog', sep=', ', end='!!!\n')
boa, cat, dog!!!

Of course, sep and end only work in Python 3's print function. For Python 2, the following is equivalent.

>>> print ', '.join(['boa', 'cat', 'dog']) + '!!!'
boa, cat, dog!!!

You can also use a backported version of the print function in Python 2:

>>> from __future__ import print_function
>>> print('boa', 'cat', 'dog', sep=', ', end='!!!\n')
boa, cat, dog!!!
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
  • It's possible to use `print` function in Python 2 http://stackoverflow.com/q/12162629/2301450 – vaultah Apr 09 '16 at 05:28
  • Python 2 has a [print statement](https://docs.python.org/2/reference/simple_stmts.html#grammar-token-print_stmt), @vaultah, that has important differences. For one thing, it doesn't support `sep` and `end`. – Don Kirkby Apr 09 '16 at 05:30
  • I'm just saying that your claim that it only works in Python 3 is incorrect. – vaultah Apr 09 '16 at 05:31
  • My claim was that only `sep` and `end` work in Python 3, @vaultah. I've clarified the answer. – Don Kirkby Apr 09 '16 at 05:33
  • 2
    Actually, Python 2's print function [supports `sep` and `end`](https://docs.python.org/2/library/functions.html#print). – vaultah Apr 09 '16 at 05:36
  • Oh right, @vaultah, I forgot about the `__future__` module. I've added that to the answer. – Don Kirkby Apr 09 '16 at 06:05
1

The following two are equivalent:

print(*array, sep='abc')
print('abc'.join(str(x) for x in array))
BallpointBen
  • 9,406
  • 1
  • 32
  • 62
0

sep='' and end='' are two different thing. ignore space and make variable as a single string.. like: a b-->ab But end='' make a b -->a b see the below example. for sep=' '

from itertools import permutations
s,k = input().split()
for i in list(permutations(sorted(s), int(k))):
    print(*i,sep='')

    ''' output for sep='': 
    HACK 2
    AC
    AH
    AK
    CA
    CH
    CK
    HA
    HC
    HK
    KA
    KC
    KH
    '''

for end=' '

    from itertools import permutations

    s, k = input().split()
    for i in list(permutations(sorted(s), int(k))):
        print(*i, end='')
    '''
HACK 2
A CA HA KC AC HC KH AH CH KK AK CK H
Process finished with exit code 0
'''
0

In array variable instead of sep you can use join

pets = ['boa', 'cat', 'dog']
res=",".join(pets)
print(res)

Output

boa,cat,dog
javasundaram
  • 877
  • 2
  • 14
  • 28