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
a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
6 ở phía trước chuỗi và viết biến bên trong niềng răng xoăn [a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
7] 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 a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
6 in front of the string and write the variable inside curly braces [a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
7] like so:Python 3.6 you can use so-called "formatted strings" [or "f strings"] to easily insert variables into your strings. Just add an a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
6 in front of the string
and write the variable inside curly braces [a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
7] like so:Nội dung chính
- Python 3: Chuỗi được định dạng
- 1. Giải pháp: ngoặc đơn
- 2. Giải pháp: Chuỗi đa dòng
- Làm thế nào để bạn kết hợp các dòng trong Python?
- Làm thế nào để bạn kết hợp 3 chuỗi trong Python?
- Làm thế nào để bạn tham gia một chuỗi đa dòng trong Python?
- Làm thế nào để tôi kết hợp nhiều chuỗi?
>>> 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 [
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] 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 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
1 hoặc 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 thay vì một].parentheses [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] or use a multi-line string [a string surrounded by three quotes 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
1 or 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 instead of one].parentheses [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]
or use a multi-line string [a string surrounded by three quotes 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
1 or 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 instead of one].1. Giải pháp: ngoặc đơn
2. Giải pháp: Chuỗi đa dò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
Làm thế nào để bạn kết hợp các dòng trong Python? If there is no variable in a line, there is no need for a leading
a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
6 for
that line.Làm thế nào để bạn kết hợp 3 chuỗi trong Python?
Làm thế nào để bạn tham gia một chuỗi đa dòng trong Python?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 [Làm thế nào để tôi kết hợp nhiều chuỗi?Để 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 [
0] hoặc sử dụng chuỗi nhiều dòng [một chuỗi được bao quanh bởi ba trích dẫna_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
1 hoặca_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 thay vì một].parentheses [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] or use a multi-line string [a string surrounded by three quotesa_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
1 ora_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 instead of one].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. Giải pháp: Chuỗi đa dòng
Làm thế nào để bạn kết hợp các dòng trong Python?
Làm thế nào để bạn kết hợp 3 chuỗi trong Python?Làm thế nào để bạn tham gia một chuỗi đa dòng trong Python? Just make sure you align your code correctly otherwise you will have leading white space in front each line.
Làm thế nào để tôi kết hợp nhiều chuỗi?
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] 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 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
1 hoặc 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 thay vì một].parentheses [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] or use a multi-line string [a string surrounded by three quotes 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
1 or 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 instead of one].4 vì đó là tên của kiểu dữ liệu. you shouldn't call your variable 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: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
6 cho dòng đó. If there is no variable in a line, there is no need for a leading a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
6 for that line.a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
- 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 [
- PEP498
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
2] at the end of each line instead of surrounding parentheses but accordingly to PEP8 you should prefer parentheses for line continuation: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
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.
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"""
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
2] at the end of each line instead of surrounding parentheses but accordingly to PEP8 you should prefer parentheses for line continuation: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
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.
Trong các chuỗi nhiều dòng, bạn không cần phải chèn rõ ràng
Examples:
3, Python chăm sóc điều đó cho bạn: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 {str1} This is line 2 {str2} This is line 3"""
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”.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. :
test_str1 = ”’
geeks for ”’
test_str2 = ”’
geeks ”’
Output :
geeks for geeks
Explanation : 1st line joined with 1st line of 2nd string, “geeks for ” -> “geeks”.
Nhân tiện: Bạn không nên gọi biến của mình
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 vì đó là tên của kiểu dữ liệu. you shouldn't call your variable 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
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
Xem thảo luậnCải thiện bài viết
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
3Lưu bài viếtO[n]
ĐọcO[n]
Làm thế nào để bạn kết hợp các dòng trong Python?
Làm thế nào để bạn kết hợp 3 chuỗi trong Python?
Python3
Làm thế nào để bạn tham gia một chuỗi đa dòng trong Python?
Làm thế nào để tôi kết hợp nhiều chuỗi?Đầu ra
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Độ phức tạp về thời gian: O [n] -> [chức năng tham gia]O[n] -> [ join function]O[n] -> [ join function]
Không gian phụ trợ: O [n]O[n]O[n]
Làm thế nào để bạn kết hợp các dòng trong Python?
Concatenation chuỗi có thể được thực hiện bằng nhiều cách ....
Sử dụng + toán tử ..
Sử dụng phương thức tham gia [] ..
Sử dụng nhà điều hành % ..
Sử dụng hàm định dạng [] ..
Sử dụng, [dấu phẩy].
Làm thế nào để bạn kết hợp 3 chuỗi trong Python?
Để nối các chuỗi, chúng tôi sử dụng toán tử +.Hãy nhớ rằng khi chúng ta làm việc với các số, + sẽ là một nhà điều hành để bổ sung, nhưng khi được sử dụng với các chuỗi, nó là một toán tử tham gia.use the + operator. Keep in mind that when we work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.use the + operator. Keep in mind that when we work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.
Làm thế nào để bạn tham gia một chuỗi đa dòng trong Python?
Sử dụng trích dẫn ba để tạo một chuỗi đa dòng bất cứ thứ gì bên trong các trích dẫn ba bên trong sẽ trở thành một phần của một chuỗi đa dòng.Chúng ta hãy có một ví dụ để minh họa hành vi này.# Chuỗi chứa các ký tự Newline line_str = "Tôi đang học Python. Anything inside the enclosing Triple quotes will become part of one multiline string. Let's have an example to illustrate this behavior. # String containing newline characters line_str = "I'm learning Python. Anything inside the enclosing Triple quotes will become part of one multiline string. Let's have an example to illustrate this behavior. # String containing newline characters line_str = "I'm learning Python.
Làm thế nào để tôi kết hợp nhiều chuỗi?
Kết nối là quá trình nối thêm một chuỗi vào cuối chuỗi khác.Bạn nối các chuỗi bằng cách sử dụng toán tử +.Đối với các chuỗi chữ và hằng số chuỗi, sự kết hợp xảy ra tại thời điểm biên dịch;Không có sự kết hợp thời gian chạy xảy ra.Đối với các biến chuỗi, việc kết hợp chỉ xảy ra tại thời điểm chạy.by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.