Python 3: Chuỗi được định dạng
Kể từ Python 3.6, bạn có thể sử dụng cái gọi là "chuỗi được định dạng" [hoặc "chuỗi F"] để dễ dàng chèn các biến vào chuỗi của bạn. Chỉ cần thêm f
ở phía trước chuỗi và viết biến bên trong niềng răng xoăn [{}
] như vậy:Python 3.6 you can use so-called "formatted strings" [or "f strings"] to easily insert variables into your strings. Just add an f
in front of the string and write the variable inside curly braces [{}
] like so:
>>> name = "John Doe"
>>> f"Hello {name}"
'Hello John Doe'
Để phân chia một chuỗi dài thành nhiều dòng bao quanh các phần với dấu ngoặc đơn [[]
] hoặc sử dụng chuỗi nhiều dòng [một chuỗi được bao quanh bởi ba trích dẫn """
hoặc '''
thay vì một].parentheses [[]
] or use a multi-line string [a string surrounded by three
quotes """
or '''
instead of one].
1. Giải pháp: ngoặc đơn
Với dấu ngoặc đơn xung quanh chuỗi của bạn, bạn thậm chí có thể kết hợp chúng mà không cần phải ký ____10 ở giữa:
a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
Tốt để biết: Nếu không có biến trong một dòng, không cần phải hàng đầu f
cho dòng đó. If there is no variable in a line, there is no need for a leading f
for that line.
Thật tốt khi biết: bạn có thể lưu trữ kết quả tương tự với các dấu gạch chéo ngược [
2] ở cuối mỗi dòng thay vì các dấu ngoặc đơn xung quanh nhưng theo đó là PEP8, bạn nên thích dấu ngoặc đơn để tiếp tục dòng: You could archive the same result with backslashes [a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
2] at the end of each line instead of surrounding parentheses but
accordingly to PEP8 you should prefer parentheses for line continuation:Các đường dài có thể được phá vỡ trên nhiều dòng bằng cách gói các biểu thức trong ngoặc đơn. Chúng nên được sử dụng theo sở thích để sử dụng dấu gạch chéo ngược để tiếp tục dòng.
2. Giải pháp: Chuỗi đa dòng
Trong các chuỗi nhiều dòng, bạn không cần phải chèn rõ ràng
a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
3, Python chăm sóc điều đó cho bạn:a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
Thật tốt khi biết: Chỉ cần đảm bảo bạn sắp xếp mã của mình một cách chính xác nếu không bạn sẽ có không gian trắng dẫn đầu ở phía trước mỗi dòng. Just make sure you align your code correctly otherwise you will have leading white space in front each line.
Nhân tiện: Bạn không nên gọi biến của mình
4 vì đó là tên của kiểu dữ liệu. you shouldn't call your variable a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
4 because that's the name of the datatype itself.Nguồn cho chuỗi được định dạng:
- Có gì mới trong Python 3.6
- PEP498
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:
Bàn luận :
test_str1 = ”’
geeks for
geeks”’,
test_str2 = ”’
is
best”’
Output :
geeks foris
geeksbest
Explanation : 1st line joined with 1st line of 2nd string, “geeks for” -> “is”.Cho cặp chuỗi, đó là đa dòng, thực hiện liên kết theo chiều ngang. :
test_str1 = ”’
geeks for ”’
test_str2 = ”’
geeks ”’
Output :
geeks for geeks
Explanation : 1st line joined with 1st line of 2nd string, “geeks for ” -> “geeks”.
Đầu vào: & nbsp; test_str1 = '' '& nbsp; geek for & nbsp; geek' ', & nbsp; test_str2 Chuỗi thứ 2, Geek Geeks cho người khác -> là là.
Đầu vào: & nbsp; test_str1 = '' '& nbsp; geek cho' ' Càng -> Geek Geek.
Python3
a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
5a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
6a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
7a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
6a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
9a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
0a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
1 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
0 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
4a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
4a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
9a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
0a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
7 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
0 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
4The original string 1 is : geeks 4 geeks The original string 2 is : is best After String Horizontal Concatenation : geeks 4is geeksbest0
Phương pháp số 1: Sử dụng zip [] + split [] + tham gia [] + danh sách hiểu hiểu
Trong đó, chúng tôi thực hiện nhiệm vụ phân tách bằng cách sử dụng split [] và chúng được ghép nối với nhau bằng zip []. Bước tiếp theo là tham gia cả hai chuỗi có khóa kéo để định hướng ngang bằng cách sử dụng \ n, và tham gia [].
a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
9a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
0f
1 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
0 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
4f
4Đầu ra
The original string 1 is : geeks 4 geeks The original string 2 is : is best After String Horizontal Concatenation : geeks 4is geeksbest
Độ phức tạp về thời gian: O [n] -> [chức năng tham gia]O[n]
Không gian phụ trợ: O [n]O[n]
Làm thế nào để bạn tham gia nhiều chuỗi trong một danh sách trong Python?
Bạn có thể kết hợp một danh sách các chuỗi vào một chuỗi với phương thức chuỗi, hãy nối []. Gọi phương thức tham gia [] từ 'chuỗi vào chèn' và truyền [danh sách các chuỗi]. Nếu bạn sử dụng một chuỗi trống '', [Danh sách các chuỗi] chỉ đơn giản là được nối và nếu bạn sử dụng dấu phẩy, thì nó sẽ tạo ra một chuỗi được phân phối bằng dấu phẩy.
Python3
f
5 f
6f
7 f
8
a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
5a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
6a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
7a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
6a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
9a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
0a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
1 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
0 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
4a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
4a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
9a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
0a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
7 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
0 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
4The original string 1 is : geeks 4 geeks The original string 2 is : is best After String Horizontal Concatenation : geeks 4is geeksbest0
The original string 1 is : geeks 4 geeks The original string 2 is : is best After String Horizontal Concatenation : geeks 4is geeksbest9
a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
6 The original string 1 is : geeks 4 geeks The original string 2 is : is best After String Horizontal Concatenation : geeks 4is geeksbest5
[]
8[]
9"""
0The original string 1 is : geeks 4 geeks The original string 2 is : is best After String Horizontal Concatenation : geeks 4is geeksbest5
The original string 1 is : geeks 4 geeks The original string 2 is : is best After String Horizontal Concatenation : geeks 4is geeksbest6
The original string 1 is : geeks 4 geeks The original string 2 is : is best After String Horizontal Concatenation : geeks 4is geeksbest5
"""
4a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
9a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
0f
1 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
0 a_str = [f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
f"This is line 3"] # no variable in this line, so a leading f"" is optional but can be used to properly align all lines
4f
4Đầu ra
The original string 1 is : geeks 4 geeks The original string 2 is : is best After String Horizontal Concatenation : geeks 4is geeksbest
Độ phức tạp về thời gian: O [n] -> [chức năng tham gia]O[n] -> [ join function]
Không gian phụ trợ: O [n]O[n]