【问题标题】:I am getting spaces in the output of my program. Any idea how to get rid of them?我的程序输出中有空格。知道如何摆脱它们吗?
【发布时间】:2021-05-16 09:18:49
【问题描述】:

我正在开发一个 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

【问题讨论】:

  • 检查ij。如果i&gt;jW[i:j] 将给出空格

标签: python string substring slice


【解决方案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

【讨论】:

    【解决方案2】:

    您可以将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
    

    【讨论】:

      【解决方案3】:

      尝试以下方法:

      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)
      

      打印这些新行的原因是因为您打印的是空字符串;想想每个ij 是什么。

      例如,当 i 可能为 3 时,您将 j 重置为 1。而W[3:1] 就是""

      【讨论】:

      • 啊,我明白了。非常感谢您的努力!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 2013-01-08
      • 2019-01-12
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多