Skip to content

Commit 298dec3

Browse files
author
Pankaj Kumar
committed
Python string append
1 parent 2615b43 commit 298dec3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def str_append(s, n):
2+
output = ''
3+
i = 0
4+
while i < n:
5+
output += s
6+
i = i + 1
7+
return output
8+
9+
10+
def str_append_list_join(s, n):
11+
l1 = []
12+
i = 0
13+
while i < n:
14+
l1.append(s)
15+
i += 1
16+
return ''.join(l1)
17+
18+
19+
if __name__ == "__main__":
20+
print('Append using + operator:', str_append('Hi', 10))
21+
print('Append using list and join():', str_append_list_join('Hi', 10))
22+
# use below for this case, above methods are created so that we can
23+
# check performance using timeit module
24+
print('Append using * operator:', 'Hi' * 10)

0 commit comments

Comments
 (0)