-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexercise_23.py
56 lines (46 loc) · 1.22 KB
/
exercise_23.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
print(
'-----------------------------------------\n'\
'Practical python education || Exercise-23:\n'\
'-----------------------------------------\n'
)
print(
'Task:\n'\
'-----------------------------------------\n'\
'Write a Python program to get the n (non-negative integer) copies of the first 2 characters of a given string. Return the n copies of the whole string if the length is less than 2."\n'
)
print(
'Solution:\n'\
'-----------------------------------------'\
)
str = input("Enter your string = ")
n = abs(int(input("Enter amount for clonning of the string = ")))
#First solution:
'''
res = ""
output = ""
if len(str) < 2:
res = str;
else:
res = str[:2]
for i in range(n):
output = output + res
print("Clonning copies from your string:")
print(output)
'''
#Second solution:
def substring_copy(str, n):
result = ""
flen = 2
if flen > len(str):
flen = len(str)
substr = str[:flen]
for i in range(n):
result = result + substr
return result
print("Clonning copies from your string:")
print(substring_copy(str, n))
print(
'\n-----------------------------------------\n'\
'Copyright 2018 Vladimir Pavlov. All Rights Reserved.\n'\
'-----------------------------------------'
)