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
print
x: 1 y: 2 1 + 2 = 30
x: 1 y: 2 1 + 2 = 31
x: 1 y: 2 1 + 2 = 32
print
x: 1 y: 2 1 + 2 = 30
x: 1 y: 2 1 + 2 = 35
x: 1 y: 2 1 + 2 = 32
print
x: 1 y: 2 1 + 2 = 30
x: 1 y: 2 1 + 2 = 39
x: 1 y: 2 1 + 2 = 32
print
x: 1 y: 2 1 + 2 = 30
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks3
x: 1 y: 2 1 + 2 = 32
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.
print
x: 1 y: 2 1 + 2 = 30
print["Hello"]
print["World."]
#the parentheses allow it to work in Python 2, or 3.
3print["Hello"]
print["World."]
#the parentheses allow it to work in Python 2, or 3.
4print
x: 1 y: 2 1 + 2 = 30
print["Hello"]
print["World."]
#the parentheses allow it to work in Python 2, or 3.
7print["Hello"]
print["World."]
#the parentheses allow it to work in Python 2, or 3.
8print
print["Hello\nWorld."]
0print["Hello\nWorld."]
1print["Hello\nWorld."]
2print["Hello\nWorld."]
3print["Hello\nWorld."]
4print["Hello\nWorld."]
5print["Hello\nWorld."]
6Output:
x: 1 y: 2 1 + 2 = 3
Geeks For Geeks
Geeks For Geeks
Geeks For Geeks
Geeks For Geeks
5Geeks For Geeks
Geeks For Geeks
Geeks For Geeks
Geeks For Geeks
6 Geeks For Geeks
Geeks For Geeks
Geeks For Geeks
Geeks For Geeks
7
Print multiple spaces between two values.
Python3
print
x: 1 y: 2 1 + 2 = 30
print["Hello\nWorld."]
9print["Hello\nWorld."]
5x: 1 y: 2 1 + 2 = 39
print["Hello\nWorld."]
5x=' ';
print["Hello"+10*x+"world"];
3print["Hello\nWorld."]
5x: 1 y: 2 1 + 2 = 39
print["Hello\nWorld."]
5print["Hello\nWorld."]
9x: 1 y: 2 1 + 2 = 32
print
x: 1 y: 2 1 + 2 = 30
print["Hello\nWorld."]
9print["Hello", "World.", sep=' '*8, end="\n"]
# you don't need to specify end, if you don't want to, but I wanted you to know it was also an option
#if you wanted to have an 8 space prefix, and did not wish to use tabs for some reason, you could do the following.
print["%sHello World." % [' '*8]]
2x=' ';
print["Hello"+10*x+"world"];
3print["Hello", "World.", sep=' '*8, end="\n"]
# you don't need to specify end, if you don't want to, but I wanted you to know it was also an option
#if you wanted to have an 8 space prefix, and did not wish to use tabs for some reason, you could do the following.
print["%sHello World." % [' '*8]]
2print["Hello\nWorld."]
9x: 1 y: 2 1 + 2 = 32
print
x: 1 y: 2 1 + 2 = 30
print["Hello\nWorld."]
9print["Hello\nWorld."]
5x: 1 y: 2 1 + 2 = 39
print["%sHello%sWorld." % [' '*8, ' '*4]]
2print["%sHello%sWorld." % [' '*8, ' '*4]]
3print["Hello\nWorld."]
5x=' ';
print["Hello"+10*x+"world"];
3print["Hello\nWorld."]
5x: 1 y: 2 1 + 2 = 39
print["%sHello%sWorld." % [' '*8, ' '*4]]
2print["%sHello%sWorld." % [' '*8, ' '*4]]
3print["Hello\nWorld."]
5print["Hello\nWorld."]
9x: 1 y: 2 1 + 2 = 32
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks8
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks6
print["Hello"]
print["World."]
#the parentheses allow it to work in Python 2, or 3.
0Output:
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks
Đầu tiên và quan trọng nhất, đối với Newlines, điều đơn giản nhất cần làm là có các câu lệnh in riêng, như thế này:
print["Hello"]
print["World."]
#the parentheses allow it to work in Python 2, or 3.
Để có một đường ngắt, và vẫn chỉ có một câu lệnh in, chỉ cần sử dụng "\ n" bên trong, như sau:
print["Hello\nWorld."]
Dưới đây, tôi giải thích không gian, thay vì ngắt dòng ...
Tôi thấy phân bổ của mọi người ở đây bằng cách sử dụng ký hiệu +, cá nhân tôi, tôi thấy xấu xí. Ví dụ về những gì tôi thấy xấu xí:
x=' ';
print["Hello"+10*x+"world"];
Ví dụ trên hiện tại là, khi tôi gõ câu trả lời được gửi lên hàng đầu này. Lập trình viên rõ ràng đang đi vào Python từ PHP là ";" Cú pháp ở cuối mỗi dòng, không cần thiết đơn giản. Lý do duy nhất mà nó không thông qua một lỗi trong Python là vì dấu chấm phẩy có thể được sử dụng trong Python, thực sự chỉ nên được sử dụng khi bạn đang cố gắng đặt hai dòng trên một, vì lý do thẩm mỹ. Bạn không nên đặt những thứ này ở cuối mỗi dòng trong Python, vì nó chỉ tăng kích thước tệp.
Cá nhân, tôi thích sử dụng ký hiệu %s. Trong Python 2.7, mà tôi thích, bạn không cần dấu ngoặc đơn, "[" và "]". Tuy nhiên, bạn nên bao gồm chúng bằng mọi cách, vì vậy tập lệnh của bạn sẽ không thông qua các lỗi, trong Python 3.x và sẽ chạy trong một trong hai.
Giả sử bạn muốn không gian của mình là 8 không gian, vậy những gì tôi sẽ làm sẽ là như sau trong Python> 3.x
print["Hello", "World.", sep=' '*8, end="\n"]
# you don't need to specify end, if you don't want to, but I wanted you to know it was also an option
#if you wanted to have an 8 space prefix, and did not wish to use tabs for some reason, you could do the following.
print["%sHello World." % [' '*8]]
Phương pháp trên cũng sẽ hoạt động trong Python 2.x, nhưng bạn không thể thêm các đối số "SEP" và "END", chúng phải được thực hiện thủ công trong Python >> prefix=' '*8 >>> sep=' '*2 >>> print["%sHello%sWorld." % [prefix, sep]] Hello World.