How to repeat a string with spaces? This is my code:
a=input("enter a word:")
b=int(input("start position:"))
c=int(input("end position:"))
d=int(input("number of repetitions:"))
x=a[b:c]
print(x*d)
If I enter : a= Door, b=1, c=3, d=2 =>
The output is:
oooo
But I would like the output to have space between repetitions:
oo oo
What is missing?
CodePudding user response:
Here the item x is repeated d times in a list which is then printed with a space as the separator:
a=input("enter a word:")
b=int(input("start position:"))
c=int(input("end position:"))
d=int(input("number of repetitions:"))
x=a[b:c]
print(*[x]*d, sep=" ")
CodePudding user response:
list slicing work like list[start:end:step]
where start
is the starting index of list and is inclusive, end
is last index 1 index till list will go as end
is exclusive not include, step is next index value then start one difference
so you need to use x=a[b:c 1]
, to make your code work