Có lẽ là một câu hỏi dễ dàng mà tôi không thể tìm thấy câu trả lời trước đây ...
Tôi đang định dạng một bảng [bằng văn bản] để trông như thế này:
Timestamp: Word Number
Số lượng ký tự giữa: sau dấu thời gian và sự khởi đầu của số là 20, bao gồm cả các ký tự trong từ [vì vậy nó vẫn được căn chỉnh]. Sử dụng Python tôi đã làm điều này:
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
Điều này hoạt động, nhưng Python ném lỗi cho tôi rằng tôi không bao giờ được sử dụng [vì không phải vậy]. Mặc dù nó không phải là một vấn đề lớn, tôi chỉ tự hỏi liệu có cách nào tốt hơn để làm như vậy không.
Edit:
Vì tôi không thể thêm câu trả lời cho điều này [vì nó được đánh dấu trùng lặp], cách tốt hơn để thay thế toàn bộ điều này là
printer = timestamp + ' ' + word.ljust[20] + score
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
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
3 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
4 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
7 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
4 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2printer = timestamp + ' ' + word.ljust[20] + score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
4 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2printer = timestamp + ' ' + word.ljust[20] + score
5 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
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.
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2GeeksForGeeks Geeks For Geeks5
GeeksForGeeks Geeks For Geeks6
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2GeeksForGeeks Geeks For Geeks9
x: 1 y: 2 1 + 2 = 30
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1x: 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 = 35
x: 1 y: 2 1 + 2 = 36
x: 1 y: 2 1 + 2 = 37
x: 1 y: 2 1 + 2 = 38
Output:
x: 1 y: 2 1 + 2 = 3
7printer = timestamp + ' ' + word.ljust[20] + score
8 printer = timestamp + ' ' + word.ljust[20] + score
9
Print multiple spaces between two values.printer = timestamp + ' ' + word.ljust[20] + score
Python3
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
x: 1 y: 2 1 + 2 = 37
printer = timestamp + ' ' + word.ljust[20] + score
1x: 1 y: 2 1 + 2 = 37
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks5
x: 1 y: 2 1 + 2 = 37
printer = timestamp + ' ' + word.ljust[20] + score
1x: 1 y: 2 1 + 2 = 37
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
4 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
Input: "my name is geeks for geeks" Output: number of spaces = 5 Input: "geeksforgeeks" Output: number of spaces=04
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks5
Input: "my name is geeks for geeks" Output: number of spaces = 5 Input: "geeksforgeeks" Output: number of spaces=04
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
4 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
x: 1 y: 2 1 + 2 = 37
printer = timestamp + ' ' + word.ljust[20] + score
1number of spaces 24
number of spaces 25
x: 1 y: 2 1 + 2 = 37
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks5
x: 1 y: 2 1 + 2 = 37
printer = timestamp + ' ' + word.ljust[20] + score
1number of spaces 24
number of spaces 25
x: 1 y: 2 1 + 2 = 37
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks1
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
4GeeksForGeeks Geeks For Geeks0
printer = timestamp + ' ' + word.ljust[20] + score
8 GeeksForGeeks Geeks For Geeks2
Output:
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks
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
Đọc
Examples:
Input: "my name is geeks for geeks" Output: number of spaces = 5 Input: "geeksforgeeks" Output: number of spaces=0
Approach:
- Bàn luận
- Đưa ra một chuỗi, nhiệm vụ là viết một chương trình Python để đếm số lượng khoảng trống trong chuỗi.
- Chuỗi đầu vào từ người dùng
- Khởi tạo biến số đếm bằng 0
- Chạy một vòng lặp I từ 0 cho đến chiều dài của chuỗi
Bên trong cho vòng lặp, kiểm tra xem s [i] == trống, sau đó tăng số lượng 1
Python3
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
01 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
02 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
03 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
04printer = timestamp + ' ' + word.ljust[20] + score
8 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
06Bên ngoài cho vòng lặp, số lượng in
Ví dụ 1:
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
24 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
04x: 1 y: 2 1 + 2 = 37
printer = timestamp + ' ' + word.ljust[20] + score
8 printer = timestamp + ' ' + word.ljust[20] + score
9 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
03 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
30 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
31 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
32printer = timestamp + ' ' + word.ljust[20] + score
8 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
34 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
37 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
38Output:
number of spaces 2
Ví dụ 2:
Python3
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
01 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
02 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
03 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
04printer = timestamp + ' ' + word.ljust[20] + score
8 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
06 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
03 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
08 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
09 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
10 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
49 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
17 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
18 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
09printer = timestamp + ' ' + word.ljust[20] + score
8printer = timestamp + ' ' + word.ljust[20] + score
8 printer = timestamp + ' ' + word.ljust[20] + score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
23 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
24 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
04x: 1 y: 2 1 + 2 = 37
printer = timestamp + ' ' + word.ljust[20] + score
8 printer = timestamp + ' ' + word.ljust[20] + score
9 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
03 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
30 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
31 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
32printer = timestamp + ' ' + word.ljust[20] + score
8 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
67 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
37 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
38Output:
number of spaces 3
Ví dụ 3: Sử dụng hàm đếm [].
Python3
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
01 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
73 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
03 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
30 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
76printer = timestamp + ' ' + word.ljust[20] + score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
4 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
18 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
80printer = timestamp + ' ' + word.ljust[20] + score
8printer = timestamp + ' ' + word.ljust[20] + score
8 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
83 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
23
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
03 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
86printer = timestamp + ' ' + word.ljust[20] + score
8 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
67 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
03 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
91 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
92 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
4Output:
Number of Spaces: 3
Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau:
Độ phức tạp về thời gian: O [n]O[n]
Độ phức tạp không gian: O [n]O[n]
Ví dụ 4: Sử dụng hàm isspace [] trong python3
Python3
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
32printer = timestamp + ' ' + word.ljust[20] + score
8 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
34 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
97printer = timestamp + ' ' + word.ljust[20] + score
8 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
06
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
08 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
09 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
10 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
49 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
03 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
18printer = timestamp + ' ' + word.ljust[20] + score
06 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
17 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
97x: 1 y: 2 1 + 2 = 37
printer = timestamp + ' ' + word.ljust[20] + score
8printer = timestamp + ' ' + word.ljust[20] + score
9 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
1 offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
2printer = timestamp + ' ' + word.ljust[20] + score
14x: 1 y: 2 1 + 2 = 37
printer = timestamp + ' ' + word.ljust[20] + score
16printer = timestamp + ' ' + word.ljust[20] + score
17Đầu ra
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
0