Hướng dẫn how do you put space between words in a spaceless string in python? - làm thế nào để bạn đặt khoảng cách giữa các từ trong một chuỗi không cách trong python?

Thêm khoảng trắng giữa các ký tự của chuỗi trong Python #

Để thêm khoảng trắng giữa các ký tự của chuỗi:

  1. Gọi phương thức join() trên một chuỗi chứa một không gian.
  2. Chuyển chuỗi như một đối số cho phương thức

    Copied!

    my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
    0.
  3. Phương thức sẽ trả về một chuỗi trong đó các ký tự được phân tách bằng một không gian.

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'

Phương thức str.join lấy một điều đáng tin cậy như một đối số và trả về một chuỗi là sự kết hợp của các chuỗi trong điều kiện có thể sử dụng được.

Khi được gọi bằng một đối số chuỗi, phương thức

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
0 sẽ thêm dấu phân cách được cung cấp giữa mỗi ký tự.

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'

Để chèn khoảng trống giữa các ký tự, hãy gọi phương thức

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
0 trên một chuỗi chứa một khoảng trống.

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'

Bạn cũng có thể thêm nhiều không gian nếu bạn cần tách các ký tự bằng nhiều không gian

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
3.

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'

Một cách tiếp cận khác là lặp lại chuỗi và thêm khoảng trống giữa các ký tự theo cách thủ công.

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'

Lưu ý rằng phương pháp này không hiệu quả hơn nhiều so với sử dụng

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
4.

Bạn có thể nhân một chuỗi với một số cụ thể để lặp lại chuỗi n lần.

Copied!

print(repr(' ' * 3)) # 👉️ ' ' print(repr('a' * 3)) # 👉️ 'aaa'

Nếu bạn cần loại bỏ các không gian theo dõi sau ký tự cuối cùng, hãy sử dụng phương pháp

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
5.

Phương thức str.strip trả về một bản sao của chuỗi với khoảng trắng dẫn đầu và dấu vết đã bị loại bỏ.

Phương thức không thay đổi chuỗi gốc, nó trả về một chuỗi mới. Chuỗi là bất biến trong Python.

Đưa ra danh sách các chuỗi, nhiệm vụ là thêm một không gian trước khi bắt đầu bằng các chữ cái viết hoa.

Đầu vào: test_list = [Hồi GFGBest ,, Forgeeks ,, và ANDCUTERSCIENCESTUDENS,] & nbsp; đầu ra: [‘GFG tốt nhất,‘ Đối với Geek, và sinh viên khoa học máy tính] : test_list = [“gfgBest”, “forGeeks”, “andComputerScienceStudents”] 
Output : [‘gfg Best’, ‘for Geeks’, ‘and Computer Science Students’] 
Explanation : Words segregated by Capitals.

Đầu vào: test_list = [máy tính củaCienceStudentsLovegfg,] & nbsp; đầu ra: [‘Sinh viên khoa học máy tính yêu thích gfg,] & nbsp; : test_list = [“ComputerScienceStudentsLoveGfg”] 
Output : [‘Computer Science Students Love Gfg’] 
Explanation : Words segregated by Capitals. 
 

Phương pháp số 1: Sử dụng LOOP + Tham gia ()

& nbsp; Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi thực hiện nhiệm vụ lặp lại tất cả các chuỗi và sau đó tất cả các ký tự trước khi thêm không gian bằng cách sử dụng vòng lặp theo cách vũ phu. Isupper () được sử dụng để kiểm tra ký tự vốn.

Python3

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
6

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
7

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
8

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
9

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
0

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
1

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
0

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
3

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
4

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
5

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
9

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
0

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
1

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
3

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
4

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
5

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
9

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
7

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
1

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
4

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
4

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
6

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
7

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
9

Copied!

print(repr(' ' * 3)) # 👉️ ' ' print(repr('a' * 3)) # 👉️ 'aaa'
0

Copied!

print(repr(' ' * 3)) # 👉️ ' ' print(repr('a' * 3)) # 👉️ 'aaa'
1

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
7

Copied!

print(repr(' ' * 3)) # 👉️ ' ' print(repr('a' * 3)) # 👉️ 'aaa'
3

Copied!

print(repr(' ' * 3)) # 👉️ ' ' print(repr('a' * 3)) # 👉️ 'aaa'
4

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
3

Copied!

print(repr(' ' * 3)) # 👉️ ' ' print(repr('a' * 3)) # 👉️ 'aaa'
6

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

print(repr(' ' * 3)) # 👉️ ' ' print(repr('a' * 3)) # 👉️ 'aaa'
8

Copied!

print(repr(' ' * 3)) # 👉️ ' ' print(repr('a' * 3)) # 👉️ 'aaa'
9
The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
0

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
4

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
5

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6
The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
4

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
5

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6
The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
9
The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
0

Đầu ra

The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']

Phương pháp số 2: Sử dụng Regex () + Danh sách hiểu biết

Sự kết hợp của các chức năng trên cũng có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi sử dụng mã Regex để kiểm tra các chữ cái trên và thực hiện bổ sung không gian và tham gia bằng cách sử dụng danh sách hiểu.

Python3

The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
1
The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
2

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
6

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
7

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
8

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
9

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
0

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
1

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
0

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
3

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
4

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
5

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
9

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
0

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
1

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
3

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
5

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6
The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
9
The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
0

Đầu ra

The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']

Phương pháp số 2: Sử dụng Regex () + Danh sách hiểu biết

Sự kết hợp của các chức năng trên cũng có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi sử dụng mã Regex để kiểm tra các chữ cái trên và thực hiện bổ sung không gian và tham gia bằng cách sử dụng danh sách hiểu.

Python3

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
6

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
7

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
8

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
9

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
0

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
1

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
0

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
3

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
4

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
5

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
9

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
0

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
1

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
3

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
4

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
5

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
9

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
7

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
1

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
7

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
34

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
4

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
4

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip() print(repr(result)) # 👉️ 'a b c d e'
6

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = '_'.join(my_str) print(result) # 👉️ 'a_b_c_d_e'
43

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
5

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
6
The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
7

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
8

Copied!

my_str = 'abcde' result = ' '.join(my_str) print(result) # 👉️ 'a b c d e'
9
The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']
0

Đầu ra

The original list : ['gfgBest', 'forGeeks', 'andComputerScience']
The space added list of strings : ['gfg Best', 'for Geeks', 'and Computer Science']


Làm thế nào để bạn thêm không gian giữa các chuỗi không gian trong Python?

Đây là 1 ...
Tạo một từ điển được sắp xếp của các chuỗi con có thể ..
Sắp xếp từ điển giảm dần theo độ dài (thực hiện 2. ....
Nếu từ điển được sắp xếp trống thì hãy trả lại chuỗi đầu vào và đánh dấu nó là vô nghĩa ..
Lặp lại thông qua tất cả các mục trong từ điển được sắp xếp ..

Làm thế nào để bạn thêm không gian giữa hai từ trong Python?

Làm thế nào để thêm một khoảng trống giữa các từ trong một chuỗi trong Python..
Tham gia (): Nếu chúng ta nhận được đầu vào làm danh sách các từ, chúng ta có thể thêm không gian giữa các từ sử dụng phương thức nối () ..
+: Chúng tôi có thể thêm hai chuỗi bằng toán tử + ..
F: Chúng ta có thể thêm khoảng trống giữa các từ và nhận một chuỗi mới bằng cú pháp F String ..

Làm thế nào để bạn đặt một khoảng trống trong một chuỗi trong Python?

Sử dụng phương thức str.ljust () để thêm khoảng trống vào cuối chuỗi, ví dụ:kết quả = my_str.ljust (6, '').Phương thức Ljust lấy tổng chiều rộng của chuỗi và ký tự điền và miếng đệm ở đầu chuỗi theo chiều rộng được chỉ định với ký tự điền được cung cấp. ljust() method to add spaces to the end of a string, e.g. result = my_str. ljust(6, ' ') . The ljust method takes the total width of the string and a fill character and pads the end of the string to the specified width with the provided fill character.

Làm cách nào để thêm một khoảng trống giữa hai chuỗi?

Để thêm hai chuỗi, chúng tôi cần một toán tử '+' để tạo một số khoảng trống giữa các chuỗi, nhưng khi bản thân chuỗi đầu tiên có một khoảng trống trong đó, không cần phải gán không gian rõ ràng.we need a '+' operator to create some space between the strings, but when the first string itself has a space with in it, there is no need to assign space explicitly.