结束后空格=“”

2024-06-07 03:08:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有如下程序:

mystring="Phyton is totally awesome"

for q in mystring[::4]:
    print(q,end="")


mystring_2="Phyton is totally awesome"

for r in mystring_2[::4]:
    print(r,end="")

当运行mystring_的结果时,只会在mystring1之后追加:

PostyeePostyee

我想这样的结果mystring2的结果将在mystring1。但我想我遗漏了什么。你知道吗


Tags: in程序forisawesomeendprintmystring
3条回答

为什么不在打印mystring后打印一条新的行? 说

mystring="Phyton is totally awesome"
for q in mystring[::4]: print(q,end="")

# print a new line
print()

mystring_2="Phyton is totally awesome"
for r in mystring_2[::4]: print(r,end="")
mystring="Phyton is totally awesome"

for q in mystring[::4]:
    print(q,end="")

# Note :
#   -
# end = "" means avoid '\n' (newline) after printing
#

# Edited
#    
# the below print will print only empty line.
#
print()


mystring_2="Phyton is totally awesome"

for r in mystring_2[::4]:
    print(r,end="")

您不需要循环,mystring[::4]返回一个字符串,这样您就可以直接打印它。你知道吗

另外请注意,如果mystring_2mystring相同,则不需要定义它。字符串是不可变的,所以切片返回一个新的字符串。你知道吗

mystring="Python is totally awesome, you are right"

print(mystring[::4])
print(mystring[::4])

或者以更简洁的形式,用一种有点古怪的语法

mystring = "Python is totally awesome, you are right"
print(*2*(mystring[::4],), sep='\n')

相关问题 更多 >