Xem thảo luận
Cải thiện bài viết
Lưu bài viết
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 = 31
x: 1 y: 2 1 + 2 = 32
x: 1 y: 2 1 + 2 = 33
x: 1 y: 2 1 + 2 = 34
x: 1 y: 2 1 + 2 = 31
x: 1 y: 2 1 + 2 = 32
x: 1 y: 2 1 + 2 = 37
x: 1 y: 2 1 + 2 = 34
x: 1 y: 2 1 + 2 = 31
x: 1 y: 2 1 + 2 = 32
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
x: 1 y: 2 1 + 2 = 34
x: 1 y: 2 1 + 2 = 31
x: 1 y: 2 1 + 2 = 32
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks5
x: 1 y: 2 1 + 2 = 34
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 = 31
x: 1 y: 2 1 + 2 = 32
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]
]
5n='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]
]
6x: 1 y: 2 1 + 2 = 31
x: 1 y: 2 1 + 2 = 32
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]
]
9WORD = '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]]
0x: 1 y: 2 1 + 2 = 31
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]]
2WORD = '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]]
3WORD = '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]]
4WORD = '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]]
5WORD = '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]]
6WORD = '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]]
7WORD = '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]]
8Output:
x: 1 y: 2 1 + 2 = 3
Geeks For Geeks
Geeks For Geeks
Geeks For Geeks
Geeks For Geeks
7Geeks 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 = 31
x: 1 y: 2 1 + 2 = 32
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]]
7Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
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]]
7print[' '] 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]]
7Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
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]]
7print[' '] print[" "] print["Hello world!"] print["Hello world"]1
x: 1 y: 2 1 + 2 = 34
x: 1 y: 2 1 + 2 = 31
x: 1 y: 2 1 + 2 = 32
print[' '] print[" "] print["Hello world!"] print["Hello world"]1
Hello world! Hello world4
print[' '] print[" "] print["Hello world!"] print["Hello world"]5
Hello world! Hello world4
print[' '] print[" "] print["Hello world!"] print["Hello world"]1
x: 1 y: 2 1 + 2 = 34
x: 1 y: 2 1 + 2 = 31
x: 1 y: 2 1 + 2 = 32
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]]
7Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
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]]
7print[' '] 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]]
7Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
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]]
7print[' '] print[" "] print["Hello world!"] print["Hello world"]1
x: 1 y: 2 1 + 2 = 34
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]
]
0Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks8
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]
]
2Output:
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 = 301. 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 = 30
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