Reputation: 31
I am working on a python program that prints the substrings of a string but the substrings to be printed are those starting with a vowel. I have applied the logic but I am getting spaces that I don't want in my output. Any help would be appreciated. The following is my code:
extract = []
W = str(input("Please enter any string: "))
vowels = ['a','e','i','o','u']
for i in W:
extract.append(i)
print(extract)
j = 1
for i in range(len(W)):
if W[i] in vowels:
while j <= len(W):
X = W[i:j]
j += 1
print(X)
j = 1
And the following is my output for a string that I entered: 'somestring'
Please enter any string: somestring
['s', 'o', 'm', 'e', 's', 't', 'r', 'i', 'n', 'g']
o
om
ome
omes
omest
omestr
omestri
omestrin
omestring
e
es
est
estr
estri
estrin
estring
i
in
ing
See the spaces I'm talking about? I don't need them. My output should be something like:
o
om
ome
omes
omest
omestr
omestri
omestrin
omestring
e
es
est
estr
estri
estrin
estring
i
in
ing
Upvotes: 0
Views: 82
Reputation: 1647
To answer the question, why do you get the spaces, this modified version of your code should help you understand. You can see the spaces generated when you get the value for slices like W[3:2] = '' I am not sure why you make the list version of the string, "extract", you don't use that.
vowels = ['a','e','i','o','u']
W = 'somestring'
j = 1
for i in range(len(W)):
if W[i] in vowels:
while j <= len(W):
X = W[i:j]
j += 1
print(f"W[{i}:{j}] is Substring: {X}")
j = 1
W[1:2] is Substring:
W[1:3] is Substring: o
W[1:4] is Substring: om
W[1:5] is Substring: ome
W[1:6] is Substring: omes
W[1:7] is Substring: omest
W[1:8] is Substring: omestr
W[1:9] is Substring: omestri
W[1:10] is Substring: omestrin
W[1:11] is Substring: omestring
W[3:2] is Substring:
W[3:3] is Substring:
W[3:4] is Substring:
W[3:5] is Substring: e
W[3:6] is Substring: es
W[3:7] is Substring: est
W[3:8] is Substring: estr
W[3:9] is Substring: estri
W[3:10] is Substring: estrin
W[3:11] is Substring: estring
W[7:2] is Substring:
W[7:3] is Substring:
W[7:4] is Substring:
W[7:5] is Substring:
W[7:6] is Substring:
W[7:7] is Substring:
W[7:8] is Substring:
W[7:9] is Substring: i
W[7:10] is Substring: in
W[7:11] is Substring: ing
Upvotes: 0
Reputation: 11
You can modify the terminating character of your python string to null and only print a new line character when X exists.
extract = []
W = str(input("Please enter any string: "))
vowels = ['a','e','i','o','u']
for i in W:
extract.append(i)
print(extract)
j = 1
for i in range(len(W)):
if W[i] in vowels:
while j <= len(W):
X = W[i:j]
j += 1
# Here are the changes
print(X, end="") #add end=""
if X != "":
print() #print new line when x exists
j = 1
Upvotes: 1
Reputation: 332
Try the following:
extract = []
W = str(input("Please enter any string: "))
vowels = ['a','e','i','o','u']
for i in W:
extract.append(i)
print(extract)
for i in range(len(W)):
j = i + 1
if W[i] in vowels:
while j <= len(W):
X = W[i:j]
j += 1
print(X)
The reason those new lines are printed is because you are printing empty strings; think about what j
is for each i
.
You reset j
back to one when i
might be 3 for example. And W[3:1]
is ""
Upvotes: 2