Hướng dẫn how do you join a multiline string in python? - làm thế nào để bạn tham gia một chuỗi nhiều dòng trong python?

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 (

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) ở 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:

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

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
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

  • Đọc
  • Bàn luận
  • 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
    
    5
    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

    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
    
    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
    
    6

    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
    
    9
    a_str = f"""This is a line
            {str1}
            This is line 2
            {str2}
            This is line 3"""
    
    0
    a_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
    
    4
    a_str = f"""This is a line
            {str1}
            This is line 2
            {str2}
            This is line 3"""
    
    4

    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
    
    9
    a_str = f"""This is a line
            {str1}
            This is line 2
            {str2}
            This is line 3"""
    
    0
    a_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
    
    4
    The original string 1 is : 
    geeks 4
    geeks
    The original string 2 is : 
    is
    best
    After String Horizontal Concatenation : 
    geeks 4is
    geeksbest
    0

    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
    
    9
    a_str = f"""This is a line
            {str1}
            This is line 2
            {str2}
            This is line 3"""
    
    0f1
    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
    
    4f4

    Đầ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

    f5 f6f7 f8

    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
    
    5
    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

    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
    
    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
    
    6

    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
    
    9
    a_str = f"""This is a line
            {str1}
            This is line 2
            {str2}
            This is line 3"""
    
    0
    a_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
    
    4
    a_str = f"""This is a line
            {str1}
            This is line 2
            {str2}
            This is line 3"""
    
    4

    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
    
    9
    a_str = f"""This is a line
            {str1}
            This is line 2
            {str2}
            This is line 3"""
    
    0
    a_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
    
    4
    The original string 1 is : 
    geeks 4
    geeks
    The original string 2 is : 
    is
    best
    After String Horizontal Concatenation : 
    geeks 4is
    geeksbest
    0

    The original string 1 is : 
    geeks 4
    geeks
    The original string 2 is : 
    is
    best
    After String Horizontal Concatenation : 
    geeks 4is
    geeksbest
    9
    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
    geeksbest
    5()8()9"""0
    The original string 1 is : 
    geeks 4
    geeks
    The original string 2 is : 
    is
    best
    After String Horizontal Concatenation : 
    geeks 4is
    geeksbest
    5
    The original string 1 is : 
    geeks 4
    geeks
    The original string 2 is : 
    is
    best
    After String Horizontal Concatenation : 
    geeks 4is
    geeksbest
    6
    The original string 1 is : 
    geeks 4
    geeks
    The original string 2 is : 
    is
    best
    After String Horizontal Concatenation : 
    geeks 4is
    geeksbest
    5"""4

    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
    
    9
    a_str = f"""This is a line
            {str1}
            This is line 2
            {str2}
            This is line 3"""
    
    0f1
    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
    
    4f4

    Đầ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)


    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.with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

    Làm cách nào để kết hợp nhiều chuỗi thành một?

    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.

    Làm thế nào để bạn kết hợp các chuỗi trong Python?

    Chúng ta có thể thực hiện kết nối chuỗi bằng cách sử dụng các cách sau:..
    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 chuỗi F (nội suy chuỗi theo nghĩa đen).

    Làm thế nào để bạn tham gia 4 chuỗi trong Python?

    Phương thức tham gia () lấy tất cả các mục trong một điều khác nhau và tham gia chúng thành một chuỗi.Một chuỗi phải được chỉ định là dấu phân cách.. A string must be specified as the separator.