Trong hướng dẫn này, chúng ta sẽ giải một chương trình python thay thế dấu cách bằng dấu gạch dưới trong chuỗi. Chúng ta sẽ thảo luận về giải pháp thay thế dấu cách bằng dấu gạch dưới trong chuỗi theo 4 cách
Phương pháp 1 – Python Thay thế khoảng trắng bằng dấu gạch dưới trong chuỗi bằng cách sử dụng replace[]
Cách đơn giản nhất để thay thế khoảng trắng bằng dấu gạch dưới trong chuỗi trong python là sử dụng phương thức thay thế chuỗi python []. Phương thức thay thế [] sẽ thay thế tất cả các khoảng trắng bằng dấu gạch dưới
Cú pháp của phương thức replace[]
string.replace[old_character, new_character, count]
Thông số
- Ký tự cũ – Ký tự cũ là tham số bắt buộc cần được chuyển. Và đối với vấn đề đã cho, ký tự cũ sẽ là khoảng trắng
- Ký tự mới – Ký tự mới là tham số bắt buộc cần được chuyển. Và đối với vấn đề đã cho, ký tự mới sẽ gạch dưới [ _ ]
- Đếm – Tham số đếm là tham số tùy chọn. Nó xác định số lần bạn muốn thay thế một ký tự cũ bằng một ký tự mới, theo mặc định, nó sẽ thay thế tất cả các ký tự cũ bằng ký tự mới
Mã Python
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
đầu ra
String with space -> My Programming Tutorial String with underscore -> My_Programming_Tutorial
cũng đọc
- Trừ phần tử Danh sách theo phần tử trong python
- 5 cách để xóa không có gì khỏi danh sách python
Phương pháp 2 – Python Thay thế khoảng trắng bằng dấu gạch dưới trong chuỗi bằng cách sử dụng split[] và join[]
Một cách khác để giải quyết vấn đề này là sử dụng phương thức string[] và join[] của python. Các bước để thay thế bằng dấu gạch dưới trong chuỗi là
- Tách chuỗi bằng khoảng trắng bằng phương thức split[]
- Tham gia danh sách các phần tử được trả về bởi phương thức split[] với dấu gạch dưới
- In chuỗi mới
Mã Python
my_string = "My Programming Tutorial"
# Using split and join to replace space with underscore
new_string = "_".join[my_string.split[" "]]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
đầu ra
String with space -> My Programming Tutorial String with underscore -> My_Programming_Tutorial
Phương pháp 3 – Python Thay thế khoảng trắng bằng dấu gạch dưới trong chuỗi bằng vòng lặp for
Chúng ta có thể thay thế dấu cách bằng dấu gạch dưới trong chuỗi bằng cách sử dụng vòng lặp for. Chúng tôi sẽ lặp lại trên mọi ký tự bằng cách sử dụng vòng lặp for và so sánh nó với khoảng trắng. Và nếu khớp chúng ta sẽ thay thế bằng dấu gạch dưới và nối ký tự vào chuỗi mới
Mã Python
my_string = "My Programming Tutorial"
new_string = ""
# using for loop
for char in my_string:
if char == " ":
new_string += "_"
else:
new_string += char
print["String with space ->", my_string]
print["String with underscore ->", new_string]
đầu ra
String with space -> My Programming Tutorial String with underscore -> My_Programming_Tutorial
Phương pháp 4 – Python Thay thế khoảng trắng bằng dấu gạch dưới trong chuỗi bằng Regex sub[]
Trong Python Biểu thức chính quy hoặc Regex là một chuỗi ký tự đặc biệt giúp khớp hoặc tìm các chuỗi khác. Để biết thêm về regex trong python, bạn có thể đọc tại đây
Phương thức sub[] là một phương thức được xây dựng trong mô-đun regex. Hàm sub[] được sử dụng để thay thế các lần xuất hiện của một chuỗi con bằng một chuỗi con khác được cung cấp làm đối số trong phương thức
cú pháp
re.sub[old_substring, new_substring, main_string]
Mã Python
________số 8đầu ra
String with space -> My Programming Tutorial String with underscore -> My_Programming_Tutorial
Phần kết luận
Ở trên, chúng ta đã thảo luận về python thay thế dấu cách bằng dấu gạch dưới theo 4 cách. Chúng ta có thể thay thế dấu cách bằng dấu gạch dưới trong python bằng cách sử dụng vòng lặp replace[], for, re. sub[] và bằng cách sử dụng các phương thức split[] và join[]
Có nhiều cách tiếp cận khác nhau để loại bỏ khoảng trắng trong một chuỗi. Cách đầu tiên là cách tiếp cận Naive, đã được thảo luận trong bài viết này. Nhưng ở đây chúng ta sẽ thảo luận về tất cả các cách tiếp cận dành riêng cho Python
Phương pháp 1. Xóa khoảng trắng khỏi chuỗi bằng hàm replace[]
Python3
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
17my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
00 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
01my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
04my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
05my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
06my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
08________ 109 ________ 100 ________ 101
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03đầu ra
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
0Phương pháp 2. Xóa khoảng trắng khỏi chuỗi bằng split[] và join[]
Đầu tiên, chúng ta sử dụng hàm split[] để trả về danh sách các từ trong chuỗi, sử dụng sep làm dấu phân cách chuỗi Python. Sau đó, chúng tôi sử dụng tham gia [] để nối iterable.
Python3
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
17my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
00 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
01my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
09my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
08________ 109 ________ 100 ________ 101
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03đầu ra
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
0Phương pháp 3. Xóa khoảng trắng khỏi chuỗi bằng Python regex
Để tìm một chuỗi hoặc một nhóm chuỗi, Biểu thức chính quy [RegEx] là một chuỗi ký tự duy nhất. Nó có thể so sánh một văn bản với một mẫu để xác định xem nó có mặt hay không. Nó cũng có thể chia một mẫu thành một hoặc nhiều mẫu con
Python3
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
17my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
08 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
09
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
00 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
01my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
00 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
05my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
06my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
07my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
08my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
09my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
08________ 109 ________ 100 ________ 101
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03đầu ra
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
0Phương pháp 4. Xóa khoảng trắng khỏi chuỗi bằng translate[]
translate[] trả về một chuỗi là một chuỗi đã sửa đổi của chuỗi đã cho theo ánh xạ dịch đã cho
Python3
String with space -> My Programming Tutorial String with underscore -> My_Programming_Tutorial10
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
08 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
09
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
00 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
01my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03 String with space -> My Programming Tutorial String with underscore -> My_Programming_Tutorial17____618
String with space -> My Programming Tutorial String with underscore -> My_Programming_Tutorial19
re.sub[old_substring, new_substring, main_string]10
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
09my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
08________ 109 ________ 100 ________ 101
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03đầu ra
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
0Phương pháp 5. Xóa khoảng trắng khỏi chuỗi bằng hàm rút gọn và câu lệnh điều kiện
Giảm chức năng lặp qua chuỗi và nó nối với phần tử của chuỗi để trả kết quả nếu nó không phải là khoảng trắng.
Python3
String with space -> My Programming Tutorial String with underscore -> My_Programming_Tutorial10
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
00my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
01 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
08 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
04
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
05my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
00 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
01my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
04my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
171my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
172 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
173my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
174my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
175my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
176 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
177my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
00 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
05my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
09my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
001 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
002my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
08________ 109 ________ 100 ________ 101
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
03đầu ra
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
0Phương pháp 6. Xóa khoảng trắng khỏi chuỗi bằng hàm lstrip[]
lstrip[] tạo một chuỗi mới bằng cách xóa khoảng trắng ở phía “trái” của chuỗi hoặc khoảng trắng ở đầu chuỗi
Python3
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
09my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
00 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
012
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
013my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
00 my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
015my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
016my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
09
my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
02my_string = "My Programming Tutorial"
# Using replace[] method to replace space with underscore
new_string = my_string.replace[" ", "_"]
print["String with space ->", my_string]
print["String with underscore ->", new_string]
019đầu ra
String with space -> My Programming Tutorial String with underscore -> My_Programming_Tutorial1
Phương pháp 7. Xóa khoảng trắng khỏi chuỗi bằng hàm rstrip[]
rstrip[] tạo một chuỗi mới bằng cách xóa khoảng trắng ở cuối. Việc loại bỏ các khoảng trắng ở phía “phải” của chuỗi giúp việc nhớ lại đơn giản hơn