为什么在使用“ end”或“ \ n”或“”(空格)语句时会有所不同?

当我仅使用以下三个语句之一进行上传时,为什么会有所不同:

  1. print("*", end=" ")

  2. print("* ")

  3. print("*\n")

http://img.mukewang.com/607e9d4800014b5409420940.jpg

仅当我在中使用“ end”时,我才能获得https://www.geeksforgeeks.org/programs-printing-pyramid-patterns-python/中所示的金字塔print("*", end=" ")。代码是:


# Function to demonstrate printing pattern


def pypart(n):

    # outer loop to handle number of rows

    # n in this case

    for i in range(0, n):


        # inner loop to handle number of columns

        # values changing acc. to outer loop

        for j in range(0, i + 1):

            # printing stars

            print("*", end=" ")


        # ending line after each row

        print("\r")



# Driver Code

pypart(3)

我们为什么需要这行:print("\r")?我没有从那条线上方的评论中得到它。


宝慕林4294392
浏览 211回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python