Hướng dẫn how do you give a space between characters in python? - làm cách nào để tạo khoảng cách giữa các ký tự trong python?

Cách pythonic

Một cách rất pythonic và thiết thực để làm điều đó là bằng cách sử dụng phương thức chuỗi join():

Show
str.join(iterable)

Các tài liệu Python chính thức cho biết:

Trả về một chuỗi là sự kết hợp của các chuỗi trong Itable ... Bộ phân cách giữa các phần tử là chuỗi cung cấp phương pháp này.

Làm thế nào để sử dụng nó?

Hãy nhớ rằng: Đây là một phương thức chuỗi.

Phương pháp này sẽ được áp dụng cho

iterable = "BINGO"
separator = " " # A whitespace character.
                # The string to which the method will be applied
separator.join(iterable)
> 'B I N G O'
0 ở trên, trong đó phản ánh chuỗi sẽ được sử dụng làm phân tách của các mục trong ITable.

Hãy có một số ví dụ thực tế!

iterable = "BINGO"
separator = " " # A whitespace character.
                # The string to which the method will be applied
separator.join(iterable)
> 'B I N G O'

Trong thực tế, bạn sẽ làm điều đó như thế này:

iterable = "BINGO"    
" ".join(iterable)
> 'B I N G O'

Nhưng hãy nhớ rằng đối số là một điều có được, giống như một chuỗi, danh sách, tuple. Mặc dù phương thức trả về một chuỗi.

iterable = ['B', 'I', 'N', 'G', 'O']    
" ".join(iterable)
> 'B I N G O'

Điều gì xảy ra nếu bạn sử dụng dấu gạch nối như một chuỗi?

iterable = ['B', 'I', 'N', 'G', 'O']    
"-".join(iterable)
> 'B-I-N-G-O'

Phương pháp số 3: Sử dụng phương thức thay thế ().

Khi bạn tìm thấy một ký tự số trong chuỗi, hãy thay thế ký tự đó bằng không gian được thêm vào bên trái và phải.

  • iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    3
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    The original string is : geeks4geeks is1for10geeks
    The space added string : geeks 4 geeks is 1 for 10 geeks
    
    9
  • The original string is : ge3eks4geeks
    The space added string : ge 3 eks 4 geeks
    
    9
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    0
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    1
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    2
  • Phương pháp số 3: Sử dụng phương thức thay thế ().

    Khi bạn tìm thấy một ký tự số trong chuỗi, hãy thay thế ký tự đó bằng không gian được thêm vào bên trái và phải.

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    3
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    The original string is : geeks4geeks is1for10geeks
    The space added string : geeks 4 geeks is 1 for 10 geeks
    
    9

    The original string is : ge3eks4geeks
    The space added string : ge 3 eks 4 geeks
    
    9
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    0
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    1
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    2
    : test_str = ‘ge3eks4geeks is1for10geeks’ 

    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    3
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    4
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    0
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    1
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    7
    : ge 3 eks 4 geeks is 1 for 10 geeks 

    Các : Numbers separated from Characters. 

    Độ phức tạp về thời gian: O (n) : test_str = ‘ge3eks4geeks’ 

    Không gian phụ trợ: O (n) : ge 3 eks 4 geeks 

    Phương pháp #4: & nbsp; sử dụng phương thức isDigit () và & nbsp; thay thế () : Numbers separated from Characters by space.

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    3
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    09

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    6
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    7
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    8
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    0
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    1

    Python3

    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    3
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    4
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    22

    Cải thiện bài viết

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    6
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    7
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    8
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    0
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    1

    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    2
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    4
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    5
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    6
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    7
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    8
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    6
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    7
    iterable = ['B', 'I', 'N', 'G', 'O']    
    " ".join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    0
    iterable = ['B', 'I', 'N', 'G', 'O']    
    "-".join(iterable)
    > 'B-I-N-G-O'
    
    2

    Đầu ra

    The original string is : geeks4geeks is1for10geeks
    The space added string :  geeks 4 geeks   is 1 for 10 geeks 
    

    Phương pháp số 2: Sử dụng regex + sub ()

    Đây là một trong những cách để giải quyết. Trong đó, chúng tôi tìm kiếm các số thay vì bảng chữ cái để thực hiện nhiệm vụ phân biệt, cách tiếp cận tương tự bằng cách sử dụng số là các tiêu chí tìm kiếm để thêm không gian.

    Python3

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    1
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    2

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    3
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    5

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    6
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    7
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    8
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    0
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    1

    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    2
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    4
    The original string is : geeks4geeks is1for10geeks
    The space added string :  geeks 4 geeks   is 1 for 10 geeks 
    
    7
    The original string is : geeks4geeks is1for10geeks
    The space added string :  geeks 4 geeks   is 1 for 10 geeks 
    
    8
    The original string is : geeks4geeks is1for10geeks
    The space added string :  geeks 4 geeks   is 1 for 10 geeks 
    
    9
    iterable = ['B', 'I', 'N', 'G', 'O']    
    " ".join(iterable)
    > 'B I N G O'
    
    6

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    6
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    7
    iterable = ['B', 'I', 'N', 'G', 'O']    
    " ".join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    0
    iterable = ['B', 'I', 'N', 'G', 'O']    
    "-".join(iterable)
    > 'B-I-N-G-O'
    
    2

    Đầu ra

    The original string is : geeks4geeks is1for10geeks
    The space added string : geeks 4 geeks is 1 for 10 geeks
    

    Phương pháp số 2: Sử dụng regex + sub ()

    Đây là một trong những cách để giải quyết. Trong đó, chúng tôi tìm kiếm các số thay vì bảng chữ cái để thực hiện nhiệm vụ phân biệt, cách tiếp cận tương tự bằng cách sử dụng số là các tiêu chí tìm kiếm để thêm không gian.

    Python3

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    1
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    2

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    6
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    7
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    8
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    0
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    1

    The original string is : ge3eks4geeks
    The space added string : ge 3 eks 4 geeks
    
    6
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    The original string is : ge3eks4geeks
    The space added string : ge 3 eks 4 geeks
    
    8

    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    2
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    4
    The original string is : geeks4geeks is1for10geeks
    The space added string :  geeks 4 geeks   is 1 for 10 geeks 
    
    7
    The original string is : geeks4geeks is1for10geeks
    The space added string :  geeks 4 geeks   is 1 for 10 geeks 
    
    8
    The original string is : geeks4geeks is1for10geeks
    The space added string :  geeks 4 geeks   is 1 for 10 geeks 
    
    9
    iterable = ['B', 'I', 'N', 'G', 'O']    
    " ".join(iterable)
    > 'B I N G O'
    
    6

    Phương pháp số 3: Sử dụng phương thức thay thế ().

    Khi bạn tìm thấy một ký tự số trong chuỗi, hãy thay thế ký tự đó bằng không gian được thêm vào bên trái và phải.

    join()8

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    9

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    6
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    7
    iterable = ['B', 'I', 'N', 'G', 'O']    
    " ".join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    0
    iterable = ['B', 'I', 'N', 'G', 'O']    
    "-".join(iterable)
    > 'B-I-N-G-O'
    
    2

    Đầu ra

    The original string is : ge3eks4geeks
    The space added string : ge 3 eks 4 geeks
    

    Phương pháp số 2: Sử dụng regex + sub ()O(n)

    Đây là một trong những cách để giải quyết. Trong đó, chúng tôi tìm kiếm các số thay vì bảng chữ cái để thực hiện nhiệm vụ phân biệt, cách tiếp cận tương tự bằng cách sử dụng số là các tiêu chí tìm kiếm để thêm không gian.O(n)

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    1
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    2

    Python3

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    3
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    5

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    6
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    7
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    8
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    0
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    1

    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    2
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    4
    The original string is : geeks4geeks is1for10geeks
    The space added string :  geeks 4 geeks   is 1 for 10 geeks 
    
    7
    The original string is : geeks4geeks is1for10geeks
    The space added string :  geeks 4 geeks   is 1 for 10 geeks 
    
    8
    The original string is : geeks4geeks is1for10geeks
    The space added string :  geeks 4 geeks   is 1 for 10 geeks 
    
    9
    iterable = ['B', 'I', 'N', 'G', 'O']    
    " ".join(iterable)
    > 'B I N G O'
    
    6

    Phương pháp số 3: Sử dụng phương thức thay thế ().

    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    8
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4join()1
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9join()4
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"    
    " ".join(iterable)
    > 'B I N G O'
    
    9join()7

    join()8

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    4
    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks
    
    9

    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    6
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    7
    iterable = ['B', 'I', 'N', 'G', 'O']    
    " ".join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    9
    iterable = "BINGO"
    separator = " " # A whitespace character.
                    # The string to which the method will be applied
    separator.join(iterable)
    > 'B I N G O'
    
    0
    iterable = ['B', 'I', 'N', 'G', 'O']    
    "-".join(iterable)
    > 'B-I-N-G-O'
    
    2

    Đầu ra

    The original string is : ge3e1ks4geeks
    The space added string : ge 3 e 1 ks 4 geeks