Skip to content

Commit

Permalink
Python list to string
Browse files Browse the repository at this point in the history
  • Loading branch information
Pankaj Kumar committed Oct 16, 2018
1 parent 6251492 commit 3a56359
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Python-3/basic_examples/list_to_string.py
@@ -0,0 +1,37 @@
l1 = [1, 2, 3]
print(l1)

l1 = ['A', 'B', 'C']
print(l1)

l1 = ['A', 'B', 'C', 1, 2, 3.5]
print(l1)

# stripping brackets
print(str(l1).strip('[]'))
print(str(l1)[1:-1])


class Data:
id = 0

def __init__(self, i):
id = i

def __str__(self):
return f'D[{self.id}]'

def __repr__(self):
return f'Data[{self.id}]'


l1 = [Data(10), Data(20)]
print(l1) # repr() function will be called on elements

# calling str function
print(', '.join(map(str, l1)))
print(', '.join(str(e) for e in l1))

print('|'.join(map(str, l1)))
print('#'.join(str(e) for e in l1))

0 comments on commit 3a56359

Please sign in to comment.