Python 如何打印这样的图案
有一个问题。 如何打印这样的图案Python 如何打印这样的图案,python,for-loop,Python,For Loop,有一个问题。 如何打印这样的图案 stackoverflow stackoverflo tackoverflo tackoverfl ackoverfl ackoverf ckoverf ckover kover kove ove ov v 我尝试使用for循环,但失败了 str = "stackoverflow" k = len(str) print(str) print(str[:(k-1)]) 我不知道如何使用fo
stackoverflow
stackoverflo
tackoverflo
tackoverfl
ackoverfl
ackoverf
ckoverf
ckover
kover
kove
ove
ov
v
我尝试使用for循环,但失败了
str = "stackoverflow"
k = len(str)
print(str)
print(str[:(k-1)])
我不知道如何使用for循环来完成它
有没有不使用for循环的方法来解决这个问题?
谢谢…请跟踪表示要打印的字符串片段的两个索引
l
和r
。然后在每次迭代中缩短该切片
s = 'stackoverflow'
l, r = 0, len(s) # range of string to print
remove_left = True # which side of the string to remove
space = 0 # how much space to print to the left
while l < r:
print('%s%s' % (' ' * int(space/2), s[l:r]))
if remove_left:
r-= 1
else:
l+= 1
remove_left = not remove_left
space += 1
另一个可能的解决办法是
s = "stackoverflow"
toggle = True # If true, remove first char. Else, cut last char.
left = 0 # number of spaces to prepend
right = 0 # number of spaces to append
while s: # while s is not empty
print(' '*left + s + ' '*right)
if toggle:
s = s[1:] # remove first char
left += 1
else:
s = s[:-1] # remove last char
right += 1
toggle = not toggle
它给出了输出
stackoverflow
tackoverflow
tackoverflo
ackoverflo
ackoverfl
ckoverfl
ckoverf
koverf
kover
over
ove
ve
v
您可以使用'some_string'。rjust(width',),其中width是一个整数值,第二个参数是一个字符串,在我的示例中使用了一个空格。也可以使用'some_string'.ljust(width')。有关更多信息,请查看此网站 例如:
def word_reduce(word):
n = word.__len__()
for i in range(n):
left = i // 2
right = i - left
result = word[left:n-right]
print((' ').rjust(left + 1) + result)
s = 'stackoverflow'
word_reduce(s)
我建议对循环使用
。一旦你习惯了,它们很容易使用。下面是一个使用for
循环的解决方案:
def show(s):
n = len(s)
for i in range(n):
n1 = i // 2
n2 = i - n1
print(" " * n1 + s[n1:n-n2])
s = "stackoverflow"
show(s)
输出为:
stackoverflow
stackoverflo
tackoverflo
tackoverfl
ackoverfl
ackoverf
ckoverf
ckover
kover
kove
ove
ov
v
如果您确实不想为
循环使用,可以使用while
循环替换它,如下所示:
i = 0
while i < n:
...
i += 1
i=0
而i
您可以仅使用一个计数器来尝试此操作
string = "stackoverflow"
loop=0
while string.replace(' ','')!='':
print(' '*(loop//2)+string+' '*(loop//2))
if loop%2==0:
string=string[:-1]
else:
string=string[1:]
loop=loop+1
您可以使用递归的概念
def stackoverflow(pattern,alternate=0):
if len(pattern) == 1:
#base condition for recursion
return
elif alternate == 0:
#first time execution
print(pattern)
alternate = alternate + 1
stackoverflow(pattern, alternate)
elif alternate % 2 != 0:
# truncate from right side
pattern = pattern[:-1]
print(pattern)
alternate = alternate + 1
stackoverflow(pattern, alternate)
else:
#truncate from left side
pattern = pattern[1:]
print(pattern)
alternate = alternate + 1
stackoverflow(pattern,alternate)
您可以只使用简单的切片运算符:
a='stackoverflow'
print(a)
#printing first whole string
for i in range(len(a)):
#loop range of the string
if i%2==0:
#here the logic see the problem you will find a pattern that it removing
#last character when loop number is even and update the string with current
#sliced string
#print(i)
# if you want use this print for understanding track of slicing
print('{:^12s}'.format(a[:-1]))
#Removing last character if loop index is even
a=a[:-1]
#update the string with current sliced string
else:
#print(i)
#use this print for tracking of sliced string
print('{:^14s}'.format(a[1:]))
#remove first character if loop index is not even or loop index is odd.
a=a[1:]
#update the current string with sliced string
输出:
stackoverflow
stackoverflo
tackoverflo
tackoverfl
ackoverfl
ackoverf
ckoverf
ckover
kover
kove
ove
ov
v
正如OP所说,没有循环。硬编码算是有效答案吗
print(“stackoverflow”)
print(“stackoverflo”)
print(“ tackoverflo”)
print(“ tackoverfl”)
print(“ ackoverfl”)
print(“ ackoverf”)
print(“ ckoverf”)
print(“ ckover”)
print(“ kover)
print(“ kove”)
print(“ ove”)
print(“ ov”)
print(“ v”)
下面是使用递归函数的另一种方法printline()
用于循环不需要
# Recursive function
def printline(string, cutleft, padindex):
# Print out the required line
print(string.rjust(padindex+len(string)))
# Last character to print out
if len(string) == 1:
return
# Deciding to trim the left or right part of the string
if cutleft:
printline(string[1:], 0, padindex + 1)
else:
printline(string[:-1], 1, padindex)
# Calling the recursive function with initial value
printline('stackoverflow', 0, 0)
这是输出
stackoverflow
stackoverflo
tackoverflo
tackoverfl
ackoverfl
ackoverf
ckoverf
ckover
kover
kove
ove
ov
v
为什么要避免使用for循环?使用for或while循环似乎是实现这一点的自然方式。是的,可能有一些不做循环的方法可以做到这一点,但是这些方法可能会让你更加困惑。然后你将不得不使用一个while循环来编写一些代码,说明你可以做什么,而不是指向一个引用
stackoverflow
stackoverflo
tackoverflo
tackoverfl
ackoverfl
ackoverf
ckoverf
ckover
kover
kove
ove
ov
v