我的程序输出中出现空格。知道如何摆脱它们吗?
I am getting spaces in the output of my program. Any idea how to get rid of them?
我正在开发一个 python 程序,该程序打印字符串的子字符串,但要打印的子字符串是以元音开头的。我已经应用了逻辑,但我在输出中得到了我不想要的空间。任何帮助,将不胜感激。以下是我的代码:
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
下面是我输入的字符串的输出:'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
看到我说的空间了吗?我不需要它们。我的输出应该是这样的:
o
om
ome
omes
omest
omestr
omestri
omestrin
omestring
e
es
est
estr
estri
estrin
estring
i
in
ing
尝试以下操作:
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)
打印这些新行的原因是因为您正在打印空字符串;想想每个 i
.
的 j
是什么
例如,当 i
可能为 3 时,您将 j
重置为 1。 W[3:1]
是 ""
您可以将 python 字符串的终止字符修改为 null,并且仅在 X 存在时打印换行符。
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
要回答这个问题,为什么会有空格,这个修改后的代码版本应该可以帮助您理解。
你可以看到当你得到像 W[3:2] = '' 这样的切片的值时生成的空间
我不确定你为什么制作字符串的列表版本,“extract”,你不使用它。
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
我正在开发一个 python 程序,该程序打印字符串的子字符串,但要打印的子字符串是以元音开头的。我已经应用了逻辑,但我在输出中得到了我不想要的空间。任何帮助,将不胜感激。以下是我的代码:
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
下面是我输入的字符串的输出:'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
看到我说的空间了吗?我不需要它们。我的输出应该是这样的:
o
om
ome
omes
omest
omestr
omestri
omestrin
omestring
e
es
est
estr
estri
estrin
estring
i
in
ing
尝试以下操作:
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)
打印这些新行的原因是因为您正在打印空字符串;想想每个 i
.
j
是什么
例如,当 i
可能为 3 时,您将 j
重置为 1。 W[3:1]
是 ""
您可以将 python 字符串的终止字符修改为 null,并且仅在 X 存在时打印换行符。
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
要回答这个问题,为什么会有空格,这个修改后的代码版本应该可以帮助您理解。 你可以看到当你得到像 W[3:2] = '' 这样的切片的值时生成的空间 我不确定你为什么制作字符串的列表版本,“extract”,你不使用它。
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