Thao tác chuỗi là một trong những khía cạnh quan trọng nhất của Python. Bạn có thể thao tác với các chuỗi bằng kỹ thuật cắt, lặp qua các phần tử và phương thức chuỗi. Có thể sử dụng các phương thức như count(), find(), format(), upper(), index() và split(). Nhưng có thể xảy ra trường hợp bạn muốn xóa ký tự đầu hoặc ký tự cuối khỏi chuỗi.
Xóa ký tự đầu tiên và cuối cùng khỏi chuỗi
Trong hướng dẫn này, bạn sẽ học cách xóa ký tự đầu tiên và ký tự cuối cùng khỏi chuỗi bằng chỉ mục
Trong Python, mỗi ký tự của chuỗi có chỉ mục của nó. Có hai loại chỉ mục trong python
- Trái sang phải hoặc Chỉ số Tích cực
- Phải sang trái hoặc Chỉ số phủ định
chuỗi
s
t
e
c
h
i
e
s
mục lục
0
1
2
3
4
5
6
7
mục lục
-số 8
-7
-6
-5
-4
-3
-2
-1
Bằng cách sử dụng chỉ mục chuỗi, chúng ta có thể xóa các ký tự cụ thể khỏi chuỗi
Xóa ký tự cuối cùng khỏi chuỗi Python
Ví dụ
# Python3 code to remove last character from string # initializing test string string='stechies' # Remove last character remove_last = string[:-1] # Print remaining string print(remove_last)đầu ra
stechieGiải trình
Trong mã này, chuỗi biến được khởi tạo với giá trị chuỗi “stechies”. Kỹ thuật cắt chuỗi được sử dụng trong dòng tiếp theo. Chuỗi [. -1] chỉ định tất cả các ký tự của chuỗi ngoại trừ ký tự cuối cùng. Chỉ số phủ định -1 chỉ định ký tự cuối cùng trong chuỗi “s”. Các [. -1] chỉ định ký tự ở chỉ mục 0 và đi lên chỉ mục trước chỉ mục cuối cùng
Câu lệnh cuối cùng của mã in ra giá trị của biến remove_last. đầu ra cuối cùng là
kỹ thuật viên
Python Xóa ký tự đầu tiên khỏi chuỗi
Ví dụ
# Python3 code to remove first character from string # initializing test string string='stechies' # Remove first character remove_first = string[1:] # Print remaining string print(remove_first)đầu ra
techiesGiải trình
Ở đây, chuỗi biến được gán giá trị 'stechies'. Biến remove_first được gán giá trị cắt của chuỗi. Chuỗi mã [1. ] chỉ định tất cả các ký tự của chuỗi bắt đầu từ chỉ mục 1 cho đến chỉ mục cuối cùng. Do đó, khi chỉ mục bắt đầu từ 0, ký tự đầu tiên của chuỗi bị xóa. Các ký tự còn lại của chuỗi được in ra màn hình trong dòng mã tiếp theo
Do đó, đầu ra cuối cùng là
kỹ thuật viên
Phần kết luận
Khi đề cập đến các chỉ số để loại bỏ các ký tự khỏi chuỗi, hãy nhớ rằng chúng bắt đầu từ 0. Vì vậy, nếu bạn đề cập đến một [1. 5], chỉ số thứ hai sẽ được xem xét chứ không phải chỉ số thứ 6. Các ký tự nằm giữa hai chỉ số sẽ được in ra màn hình. Hơn nữa, nếu bạn đề cập đến một [1. ], các phần tử từ chỉ số thứ hai cho đến chỉ số cuối cùng sẽ được in ra. Các chỉ số tiêu cực cũng nên được sử dụng cẩn thận. Điều này là để đảm bảo rằng đầu ra cuối cùng là chính xác.
Trong hướng dẫn này, chúng ta sẽ tìm hiểu về cách xóa n ký tự cuối cùng của một chuỗi trong Python
xem xét, chúng tôi có một chuỗi sau
str = "How are you"
Bây giờ, chúng tôi muốn xóa 3 ký tự cuối cùng you khỏi chuỗi trên
Xóa n ký tự cuối cùng
Chúng ta có thể xóa n ký tự cuối cùng của một chuỗi bằng cách sử dụng ký hiệu lát cắt [ ] với đối số là :-n
-n là số ký tự chúng ta cần loại bỏ từ cuối chuỗi
Đây là một ví dụ, xóa 3 ký tự cuối cùng khỏi chuỗi sau
str = "How are you" modified = str[:-3] print(modified)
đầu ra
"How are"
Ngoài ra, chúng ta cũng có thể sử dụng phương thức rstrip() bằng cách chuyển n ký tự cuối cùng làm đối số cho nó
Bài viết này trình bày một vấn đề như vậy về việc xóa ký tự thứ i khỏi chuỗi và nói về các giải pháp khả thi có thể được sử dụng để đạt được chúng bằng Python
Xóa ký tự thứ i khỏi chuỗi bằng phương thức gốc
Trong phương pháp này, người ta chỉ cần chạy một vòng lặp Python và nối thêm các ký tự khi chúng xuất hiện và tạo một chuỗi mới từ chuỗi hiện có trừ khi chỉ mục là i.
Python3
test_str= "GeeksForGeeks"
# Removing char at pos 3
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks0= The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks2
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks3 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks4_______7_______5 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks6The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks7The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks8The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks9
The string after removal of i'th character : GeksForGeeks0The string after removal of i'th character : GeksForGeeks1 The string after removal of i'th character : GeksForGeeks2= The string after removal of i'th character : GeksForGeeks4The string after removal of i'th character : GeksForGeeks5
The string after removal of i'th character : GeksForGeeks6The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks0= The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks0The string after removal of i'th character : GeksForGeeks0 The string after removal of i'th character : GeksForGeeks1
The string after removal of i'th character : GeksForGeeks2
The string after removal of i'th character : GeksForGeeks3 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks7The string after removal of i'th character : GeksForGeeks5 The string after removal of i'th character : GeksForGeeks0 The string after removal of i'th character : GeksForGeeks7
đầu ra
The string after removal of i'th character : GeksForGeeksXóa ký tự thứ i khỏi chuỗi bằng cách sử dụng str. thay thế()
các str. replace() có thể được sử dụng để thực hiện tác vụ xóa vì chúng ta có thể thay thế chỉ mục cụ thể bằng char trống và do đó giải quyết được sự cố.
Hạn chế. Hạn chế chính của phương pháp này là nó không thành công trong trường hợp có các bản sao trong một chuỗi khớp với char tại vị trí. Tôi. replace() thay thế tất cả các lần xuất hiện của một ký tự cụ thể và do đó sẽ thay thế tất cả các lần xuất hiện của tất cả các ký tự tại vị trí i. Đôi khi chúng ta vẫn có thể sử dụng chức năng này nếu ký tự thay thế xuất hiện lần đầu tiên trong chuỗi.
Python3
The string after removal of i'th character : GeksForGeeks8
test_str= "GeeksForGeeks"
# Removing char at pos 3
GeeksForGeeks3
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks0= GeeksForGeeks6GeeksForGeeks7_______34_______8
The string after removal of i'th character : GeksForGeeks2
The string after removal of ith character: GeksForGeeks0
The string after removal of i'th character : GeksForGeeks3 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks7The string after removal of ith character: GeksForGeeks3 The string after removal of i'th character : GeksForGeeks0 The string after removal of i'th character : GeksForGeeks7
The string after removal of ith character: GeksForGeeks6
The string after removal of ith character: GeksForGeeks7
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks0= GeeksForGeeks6test_str1_______142_______2test_str3test_str4
The string after removal of i'th character : GeksForGeeks2
test_str6
The string after removal of i'th character : GeksForGeeks3 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks7test_str9 The string after removal of i'th character : GeksForGeeks0 The string after removal of i'th character : GeksForGeeks7
đầu ra
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeksXóa ký tự thứ i khỏi chuỗi bằng cách sử dụng lát cắt + nối
Người ta có thể sử dụng cắt chuỗi và cắt chuỗi trước pos i và cắt sau pos i. Sau đó, sử dụng phép nối chuỗi của cả hai, ký tự thứ i có thể bị xóa khỏi chuỗi.
Python3
The string after removal of i'th character : GeksForGeeks8
test_str= "GeeksForGeeks"
# Removing char at pos 3
=7
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks0= "GeeksForGeeks"0The string after removal of i'th character : GeksForGeeks4"GeeksForGeeks"2The string after removal of i'th character : GeksForGeeks0 "GeeksForGeeks"4"GeeksForGeeks"5"GeeksForGeeks"6
The string after removal of i'th character : GeksForGeeks2
"GeeksForGeeks"8
The string after removal of i'th character : GeksForGeeks3 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks7The string after removal of i'th character : GeksForGeeks5 The string after removal of i'th character : GeksForGeeks0 The string after removal of i'th character : GeksForGeeks7
đầu ra
The string after removal of i'th character : GeksForGeeksXóa ký tự thứ i khỏi chuỗi bằng cách sử dụng str. tham gia () và hiểu danh sách
Trong phương pháp này, trước tiên, mỗi phần tử của chuỗi được chuyển đổi thành từng phần tử của danh sách, sau đó mỗi phần tử trong số chúng được nối để tạo thành một chuỗi ngoại trừ chỉ mục đã chỉ định.
Python3
The string after removal of i'th character : GeksForGeeks8
test_str= "GeeksForGeeks"
# Removing char at pos 3
# Removing char at pos 39
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks0= The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks02_______7_______3 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks4The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks5 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks6The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks7The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks8The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks09The string after removal of i'th character : GeksForGeeks1 The string after removal of i'th character : GeksForGeeks2= The string after removal of i'th character : GeksForGeeks4The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks14
The string after removal of i'th character : GeksForGeeks2
"GeeksForGeeks"8
The string after removal of i'th character : GeksForGeeks3 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks7The string after removal of i'th character : GeksForGeeks5 The string after removal of i'th character : GeksForGeeks0 The string after removal of i'th character : GeksForGeeks7
đầu ra.
The string after removal of i'th character : GeksForGeeksXóa ký tự cụ thể khỏi chuỗi bằng translate()
Trong phương pháp này, chúng tôi đã xóa 123 khỏi Geeksforgeeks bằng cách sử dụng chuỗi. dịch().
Python3
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks22 = The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks24
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks25
The string after removal of i'th character : GeksForGeeks3_______7_______7The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks22The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks29The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks30The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks31The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks32 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks3 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks4The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks5 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks36The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks37
đầu ra
GeeksForGeeksSử dụng đệ quy
Để sử dụng đệ quy để xóa ký tự thứ i khỏi một chuỗi, bạn có thể định nghĩa một hàm đệ quy nhận vào chuỗi và chỉ mục cần xóa làm đối số. Hàm có thể kiểm tra xem chỉ mục có bằng 0 hay không, trong trường hợp đó, hàm sẽ trả về chuỗi đã xóa ký tự đầu tiên. Nếu chỉ số không phải là 0, hàm có thể trả về ký tự đầu tiên của chuỗi được nối với kết quả của việc gọi lại hàm trên chuỗi có chỉ số giảm đi 1
Đây là một ví dụ về cách bạn có thể thực hiện phương pháp này
Python3
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks38 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks39
The string after removal of i'th character : GeksForGeeks0The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks41
The string after removal of i'th character : GeksForGeeks0The string after removal of i'th character : GeksForGeeks1 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks4== The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks47The string after removal of i'th character : GeksForGeeks5
The string after removal of i'th character : GeksForGeeks6The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks50 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks51test_str3"GeeksForGeeks"6
The string after removal of i'th character : GeksForGeeks0
The string after removal of i'th character : GeksForGeeks0The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks56
The string after removal of i'th character : GeksForGeeks0The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks50 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks51The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks47"GeeksForGeeks"2The string after removal of i'th character : GeksForGeeks0 The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks63test_str3The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks65The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks66 test_str3test_str4
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks69
test_str= "GeeksForGeeks"
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks0= The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks75The string after removal of i'th character : GeksForGeeks4test_str4
The string after removal of i'th character : GeksForGeeks3_______7_______7The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks80The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks81
The string after removal of i'th character( doesn't work) : GksForGks The string after removal of i'th character(works) : GeekForGeeks82
Đầu raThe string after removal of ith character: GeksForGeeks
Xét về độ phức tạp về thời gian, cách tiếp cận này có độ phức tạp về thời gian là O(n) trong đó n là độ dài của chuỗi, bởi vì hàm thực hiện một lệnh gọi đệ quy duy nhất cho mỗi ký tự trong chuỗi
Về độ phức tạp của không gian, cách tiếp cận này có độ phức tạp của không gian là O(n) vì hàm thực hiện lệnh gọi đệ quy cho từng ký tự trong chuỗi và mỗi lệnh gọi yêu cầu không gian trên ngăn xếp cuộc gọi