Hướng dẫn how do you print a space in a pattern in python? - làm thế nào để bạn in một khoảng trắng trong một mẫu trong python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    ĐọcSpacing in Python language is quite simple than other programming language. In C languages, to print 10 spaces a loop is used while in python no loop is used to print number of spaces.

    Bàn luận

    Trong bài viết này, chúng tôi sẽ tìm hiểu về cách in không gian hoặc nhiều không gian trong ngôn ngữ lập trình Python. Khoảng cách trong ngôn ngữ Python khá đơn giản so với ngôn ngữ lập trình khác. Trong các ngôn ngữ C, để in 10 không gian, một vòng lặp được sử dụng trong khi ở Python, không có vòng lặp không được sử dụng để in số không gian.A simple way to print spaces

    Python3

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    2
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    3
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    4

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    2
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    7
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    4

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    2
    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    4

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    2
    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    
    5
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    4

    Output:

    GeeksForGeeks
     
     
    Geeks  For    Geeks
    
    
    

    Sau đây là ví dụ về các không gian in: Printing spaces between two values while printing in a single print statement.

    Python3

    Ví dụ 1: Một cách đơn giản để in không gian

    Ví dụ 2: In không gian in giữa hai giá trị trong khi in trong một câu lệnh in.

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    2
    n='apple'
    for i in range(len(n)):
        print(
            '  ' * i  # '', '  ', '    '...
            + n[i]  # 'a', 'p', 'p', 'l', 'e'
            + '  ' * (
                len(n) - (i + 1)  # (5 - 1), (5 - 2), (5 - 3) , (5 - 4), (5 - 5): 4, 3, 2, 1, 0
            )
            + n[(len(n) - 1) - i]
        )
    
    5
    n='apple'
    for i in range(len(n)):
        print(
            '  ' * i  # '', '  ', '    '...
            + n[i]  # 'a', 'p', 'p', 'l', 'e'
            + '  ' * (
                len(n) - (i + 1)  # (5 - 1), (5 - 2), (5 - 3) , (5 - 4), (5 - 5): 4, 3, 2, 1, 0
            )
            + n[(len(n) - 1) - i]
        )
    
    6

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    2
    n='apple'
    for i in range(len(n)):
        print(
            '  ' * i  # '', '  ', '    '...
            + n[i]  # 'a', 'p', 'p', 'l', 'e'
            + '  ' * (
                len(n) - (i + 1)  # (5 - 1), (5 - 2), (5 - 3) , (5 - 4), (5 - 5): 4, 3, 2, 1, 0
            )
            + n[(len(n) - 1) - i]
        )
    
    9
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    0

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    1
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    2
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    3
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    4
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    5
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    6
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    7
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    8

    Output:

    x: 1
    y: 2
    1 + 2 = 3
    
    
    

    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    
    7
    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    
    8
    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    
    9
    Print multiple spaces between two values.

    Python3

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    2
    print(' ')
    print(" ")
    print("Hello world!")
    print("Hello         world")
    
    1
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    7
    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    
    1
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    7
    print(' ')
    print(" ")
    print("Hello world!")
    print("Hello         world")
    
    5
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    7
    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    
    1
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    7
    print(' ')
    print(" ")
    print("Hello world!")
    print("Hello         world")
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    4

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    2
    print(' ')
    print(" ")
    print("Hello world!")
    print("Hello         world")
    
    1
     
     
    Hello world!
    Hello         world
    
    4
    print(' ')
    print(" ")
    print("Hello world!")
    print("Hello         world")
    
    5
     
     
    Hello world!
    Hello         world
    
    4
    print(' ')
    print(" ")
    print("Hello world!")
    print("Hello         world")
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    4

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    2
    print(' ')
    print(" ")
    print("Hello world!")
    print("Hello         world")
    
    1
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    7
    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    
    1
    x = 10
    y = 20
    
    print("x:",x)
    print("y:",y)
    
    4
    x = 10
    y = 20
    
    print("x:",x)
    print("y:",y)
    
    5
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    7
    print(' ')
    print(" ")
    print("Hello world!")
    print("Hello         world")
    
    5
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    7
    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    
    1
    x = 10
    y = 20
    
    print("x:",x)
    print("y:",y)
    
    4
    x = 10
    y = 20
    
    print("x:",x)
    print("y:",y)
    
    5
    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    
    7
    print(' ')
    print(" ")
    print("Hello world!")
    print("Hello         world")
    
    1
    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    4

    n='apple'
    for i in range(len(n)):
        print(
            '  ' * i  # '', '  ', '    '...
            + n[i]  # 'a', 'p', 'p', 'l', 'e'
            + '  ' * (
                len(n) - (i + 1)  # (5 - 1), (5 - 2), (5 - 3) , (5 - 4), (5 - 5): 4, 3, 2, 1, 0
            )
            + n[(len(n) - 1) - i]
        )
    
    0
    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    
    8
    n='apple'
    for i in range(len(n)):
        print(
            '  ' * i  # '', '  ', '    '...
            + n[i]  # 'a', 'p', 'p', 'l', 'e'
            + '  ' * (
                len(n) - (i + 1)  # (5 - 1), (5 - 2), (5 - 3) , (5 - 4), (5 - 5): 4, 3, 2, 1, 0
            )
            + n[(len(n) - 1) - i]
        )
    
    2

    Output:

    Geeks For Geeks
    Geeks For Geeks
    Geeks   For   Geeks
    Geeks     For          Geeks
    
    
    

    Tôi thấy rằng mã được quá nhiều. Tôi đã có thể chia nó thành các dòng riêng biệt như vậy:

    n='apple'
    for i in range(len(n)):
        print(
            '  ' * i  # '', '  ', '    '...
            + n[i]  # 'a', 'p', 'p', 'l', 'e'
            + '  ' * (
                len(n) - (i + 1)  # (5 - 1), (5 - 2), (5 - 3) , (5 - 4), (5 - 5): 4, 3, 2, 1, 0
            )
            + n[(len(n) - 1) - i]
        )
    

    Vì vậy, vấn đề ở đây là dòng này

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    01. Nó làm cho thuật toán nhân các khoảng trống trống sau chữ in đầu tiên bất kể chữ cái thứ hai (đó là chữ in Apple về phía sau): Thứ nhất, nó sẽ là 8 khoảng trắng, sau đó là 6, sau đó 4, 2, 0.

    Tôi khuyên bạn nên chia hàng đơn hàng duy nhất này thành các bước riêng biệt để hiểu những gì đang diễn ra ở đây. Như vậy:

    WORD = 'apple'
    SPACES = '  '
    SIZE = len(SPACES * len(WORD))
    TEMPLATE = list(' ' * SIZE)
    
    for i, first_char in enumerate(WORD):
        second_char = WORD[len(WORD) - 1 - i]
    
        first_char_position = len(SPACES * i)
        second_char_position = len(TEMPLATE) - len(SPACES * i) - 2
    
        curr_row = TEMPLATE.copy()
        curr_row[first_char_position] = first_char
        curr_row[second_char_position] = second_char
    
        print(''.join(curr_row))
    

    Trang chủ »Python» Chương trình Python

    Python | Không gian in: Ở đây, chúng ta sẽ học cách in một không gian/ nhiều không gian trong ngôn ngữ lập trình Python? Được gửi bởi bao gồm bao gồm, vào ngày 25 tháng 4 năm 2020: Here, we are going to learn how to print a space/ multiple spaces in the Python programming language?
    Submitted by IncludeHelp, on April 25, 2020

    Trong khi viết mã, đôi khi chúng ta cần in không gian. Ví dụ: in không gian giữa thông điệp và các giá trị, in không gian giữa hai giá trị, v.v. Trong ngôn ngữ lập trình Python, thật dễ dàng để in không gian.

    Sau đây là các ví dụ chứng minh làm thế nào để in các không gian trong Python?how to print the spaces in Python?

    Ví dụ 1: Một cách đơn giản để in không gian

    Cách đơn giản là cung cấp không gian giữa thông điệp bất cứ nơi nào chúng ta cần in không gian vào đầu ra.

    print(' ')
    print(" ")
    print("Hello world!")
    print("Hello         world")
    

    Đầu ra

     
     
    Hello world!
    Hello         world
    

    Ví dụ 2: In không gian in giữa hai giá trị trong khi in trong một câu lệnh in duy nhất

    Khi chúng tôi in nhiều giá trị được phân tách bằng các lệnh bằng cách sử dụng câu lệnh in - Python mặc định in a giữa chúng.

    x = 10
    y = 20
    
    print("x:",x)
    print("y:",y)
    

    Đầu ra

    x: 10
    y: 20
    

    Ví dụ 2: In không gian in giữa hai giá trị trong khi in trong một câu lệnh in duy nhất

    Khi chúng tôi in nhiều giá trị được phân tách bằng các lệnh bằng cách sử dụng câu lệnh in - Python mặc định in a giữa chúng.

    x = 10
    y = 20
    
    space = ' '
    
    '''
      2 spaces would be here
      1 space after first parameter,
      1 space by using space variable
      1 space after second parameter
    '''
    print("Hello",space,"world")
    
    '''
      7 spaces would be here
      1 space after first parameter,
      5 space by using space*5
      1 space after second parameter
    '''
    print("Hello",space*5,"world")
    
    '''
      12 spaces would be here
      1 space after first parameter,
      10 space by using space*10
      1 space after second parameter
    '''
    print("Hello",space*10,"world")
    
    # for better better understanding
    # assign space by any other character
    # Here, I am assigning '#'
    space = '#'
    print("Hello",space,"world")
    print("Hello",space*5,"world")
    print("Hello",space*10,"world")
    
    # printing values of x and y with spaces
    space = ' '
    print("x:",space*10,x)
    print("y:",space*10,y)
    

    Đầu ra

    x: 1
    y: 2
    1 + 2 = 3
    
    
    
    0

    Ví dụ 2: In không gian in giữa hai giá trị trong khi in trong một câu lệnh in duy nhất



    Làm thế nào để bạn in khoảng cách trong Python?

    Khoảng cách trong ngôn ngữ Python khá đơn giản so với ngôn ngữ lập trình khác. Trong các ngôn ngữ C, để in 10 không gian, một vòng lặp được sử dụng trong khi ở Python, không có vòng lặp không được sử dụng để in số không gian.no loop is used to print number of spaces.

    Làm thế nào để bạn đặt một khoảng trống vào một vòng lặp trong Python?

    1 câu trả lời..
    sử dụng end = "" và chèn các khoảng trắng theo cách thủ công ..
    Tạo một chuỗi và in sau vòng lặp: s = "" cho n trong phạm vi (n, n+7): s+= str (n)+"" s = s [:-1] ).
    Mà tôi đề nghị: Sử dụng sys.stdout.write thay vì in: in chỉ hiển thị thông báo sau khi inBreak được in ..

    Làm thế nào để bạn đặt một khoảng trống giữa văn bản trong Python?

    Để thêm khoảng trắng giữa các ký tự của chuỗi: gọi phương thức Jop () trên một chuỗi chứa một khoảng trống. Nhớ chuỗi làm đối số cho phương thức tham gia.Phương thức sẽ trả về một chuỗi trong đó các ký tự được phân tách bằng một không gian.Call the join() method on a string containing a space. Pass the string as an argument to the join method. The method will return a string where the characters are separated by a space.

    Làm thế nào để bạn in một mẫu trong Python?

    Các mẫu có thể được in bằng Python bằng cách sử dụng đơn giản cho các vòng lặp.Vòng lặp bên ngoài đầu tiên được sử dụng để xử lý số lượng hàng và vòng lồng bên trong được sử dụng để xử lý số lượng cột.Thao tác các câu lệnh in, các mẫu số khác nhau, mẫu bảng chữ cái hoặc mẫu sao có thể được in.using simple for loops. First outer loop is used to handle the number of rows and the Inner nested loop is used to handle the number of columns. Manipulating the print statements, different number patterns, alphabet patterns, or star patterns can be printed.