Viết chương trình python để thay thế khoảng trắng bằng dấu gạch dưới và ngược lại

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

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)
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)
02
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)
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)
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)
05
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)
06

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

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

________ 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)
02
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)
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)
0

Phươ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)
17

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

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

________ 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)
02
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)
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)
0

Phươ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)
17

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)
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)
02
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)
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)
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)
05
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)
06
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)
07
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)
02
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)
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)
02

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

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

________ 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)
02
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)
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)
0

Phươ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_Tutorial
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)
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)
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)
02
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)
03
String with space -> My Programming Tutorial
String with underscore -> My_Programming_Tutorial
17____618
String with space -> My Programming Tutorial
String with underscore -> My_Programming_Tutorial
19
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)
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)
02

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

________ 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)
02
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)
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)
0

Phươ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_Tutorial
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)
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)
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)
02
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)
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)
05

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)
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)
02
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)
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)
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)
171
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)
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)
173
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)
174
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)
175
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)
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)
177
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)
05
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)
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)
002

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

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

________ 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)
02
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)
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)
0

Phươ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)
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)
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)
013
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)
015
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)
016
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)
02
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)
019

đầu ra

String with space -> My Programming Tutorial
String with underscore -> My_Programming_Tutorial
1

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

Làm cách nào để thay thế khoảng trắng bằng dấu gạch dưới trong javascript?

Để thay thế khoảng trắng trong chuỗi trên bằng dấu gạch dưới, dùng split() cùng với join() .

Thay thế hoạt động như thế nào trong Python?

Các. phương thức replace() trả về một bản sao của chuỗi . Điều này có nghĩa là chuỗi con cũ vẫn giữ nguyên, nhưng một bản sao mới được tạo - với tất cả văn bản cũ đã được thay thế bằng văn bản mới.