Hướng dẫn remove the nth index character from a non-empty string in python - xóa ký tự chỉ mục thứ n khỏi chuỗi không trống trong python

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:

    Input: str = "Stable"
    Output: Modified string after removing  4 th character 
    Stabe
     
    Input: str = "Arrow"
    Output: Modified string after removing  4 th character 
    Arro

    Bàn luậnfirst approach uses a new string variable for storing the modified string. We keep a track of the characters of the string and as soon as we encounter a character at nth index, we don’t copy it to the modified string variable. Else, we copy it to a new variable. 

    Python3

    Cho một chuỗi, tác vụ là viết một chương trình Python để xóa ký tự chỉ mục thứ n khỏi chuỗi không trống

    Cách tiếp cận đầu tiên sử dụng một biến chuỗi mới để lưu trữ chuỗi sửa đổi. Chúng tôi theo dõi các ký tự của chuỗi và ngay khi chúng tôi gặp một ký tự ở chỉ mục thứ n, chúng tôi không sao chép nó vào biến chuỗi đã sửa đổi. Khác, chúng tôi sao chép nó vào một biến mới. & NBSP;

    str = "Geeksforgeeks is fun."

    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    0=
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    2

    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    3=
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    5

    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    6
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    7
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    8
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    9
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    0
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    1
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    2223____
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    0__7

    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    8
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    0
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    0
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    1
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    2
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    3

    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    8
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    5

    Output:

    Modified string after removing  4 th character  
    Geekforgeeks is fun.

    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    7
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    8
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    9=
    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    1
    O(n), where n is the length of the string. 

    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    2
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    3
    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    4= str
    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    7
    O(n)

    Độ phức tạp thời gian = o (n), trong đó n là độ dài của chuỗi. & Nbsp;second approach uses the idea of extraction of a sequence of characters within a range of index values. The syntax used in Python is as follows : 

    Không gian phụ trợ: O (n) 
    – extracts the characters starting at start_index 
    and less than end_index, that is, up to end_index-1. 
    If we don’t specify the end_index, it computes till the length of the string.

    Cách tiếp cận thứ hai sử dụng ý tưởng trích xuất một chuỗi các ký tự trong một loạt các giá trị chỉ mục. Cú pháp được sử dụng trong Python như sau: & nbsp;

    Python3

    Cho một chuỗi, tác vụ là viết một chương trình Python để xóa ký tự chỉ mục thứ n khỏi chuỗi không trống

    Cách tiếp cận đầu tiên sử dụng một biến chuỗi mới để lưu trữ chuỗi sửa đổi. Chúng tôi theo dõi các ký tự của chuỗi và ngay khi chúng tôi gặp một ký tự ở chỉ mục thứ n, chúng tôi không sao chép nó vào biến chuỗi đã sửa đổi. Khác, chúng tôi sao chép nó vào một biến mới. & NBSP;

    str = "Geeksforgeeks is fun."

    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    0=
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    2

    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    8
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    0
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    0
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    1
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    2
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    3

    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    8str2
    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    4str4

    Output:

    Modified string after removing  8 th character  
    Geeksforeeks is fun.

    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    3=
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    5
    O(n), where n is the length of the string.

    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    6
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    7
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    8
    Modified string after removing  4 th character  
    Geekforgeeks is fun.
    9
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    0
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    1
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    2223____
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    0__7
    O(n)

    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    7
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    8
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    9=
    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    1

    Python3

    Cho một chuỗi, tác vụ là viết một chương trình Python để xóa ký tự chỉ mục thứ n khỏi chuỗi không trống

    Cách tiếp cận đầu tiên sử dụng một biến chuỗi mới để lưu trữ chuỗi sửa đổi. Chúng tôi theo dõi các ký tự của chuỗi và ngay khi chúng tôi gặp một ký tự ở chỉ mục thứ n, chúng tôi không sao chép nó vào biến chuỗi đã sửa đổi. Khác, chúng tôi sao chép nó vào một biến mới. & NBSP;

    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    8
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    0
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    0
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    1
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    2
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    3

    str=str"Geeksforgeeks is fun."0str"Geeksforgeeks is fun."2

    >>> import collections
    >>> Q = collections.deque()
    >>> Q.append(1)
    >>> Q.appendleft(2)
    >>> Q.extend([3, 4])
    >>> Q.extendleft([5, 6])
    >>> Q
    deque([6, 5, 2, 1, 3, 4])
    >>> Q.pop()
    4
    >>> Q.popleft()
    6
    >>> Q
    deque([5, 2, 1, 3])
    >>> Q.rotate(3)
    >>> Q
    deque([2, 1, 3, 5])
    >>> Q.rotate(-3)
    >>> Q
    deque([5, 2, 1, 3])
    
    >>> last_three = collections.deque(maxlen=3)
    >>> for i in range(4):
    ...     last_three.append(i)
    ...     print ', '.join(str(x) for x in last_three)
    ...
    0
    0, 1
    0, 1, 2
    1, 2, 3
    2, 3, 4
    
    3
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    3

    Modified string after removing  8 th character 
    Geeksforeeks is fun.
    8
    Modified string after removing  8 th character  
    Geeksforeeks is fun.
    0str
    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    
    3

    str = "Geeksforgeeks is fun."

    Modified string after removing  8 th character 
    Geeksforeeks is fun.


    Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:50:47 (UTC/GMT +8 giờ)

    Chuỗi Python: Tập thể dục-9 với giải pháp

    Viết một chương trình Python để xóa ký tự chỉ mục thứ n khỏi chuỗi không trống.

    Hướng dẫn remove the nth index character from a non-empty string in python - xóa ký tự chỉ mục thứ n khỏi chuỗi không trống trong python

    Giải pháp mẫu:-:-

    Mã Python:

    def remove_char(str, n):
          first_part = str[:n] 
          last_part = str[n+1:]
          return first_part + last_part
    print(remove_char('Python', 0))
    print(remove_char('Python', 3))
    print(remove_char('Python', 5))
    
    

    Đầu ra mẫu:

    ython                                                                                                         
    Pyton                                                                                                         
    Pytho  
    

    Flowchart:

    Hướng dẫn remove the nth index character from a non-empty string in python - xóa ký tự chỉ mục thứ n khỏi chuỗi không trống trong python

    Trực quan hóa thực thi mã Python:

    Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

    Trình chỉnh sửa mã Python:

    Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.

    Trước đây: Viết hàm Python lấy một danh sách các từ và trả lại từ dài nhất và độ dài của đoạn dài nhất. Write a Python function that takes a list of words and return the longest word and the length of the longest one.
    Next: Write a Python program to change a given string to a new string where the first and last chars have been exchanged.

    Python: Lời khuyên trong ngày

    Cấu trúc Deques (Deques là một khái quát của các ngăn xếp và hàng đợi):

    >>> import collections
    >>> Q = collections.deque()
    >>> Q.append(1)
    >>> Q.appendleft(2)
    >>> Q.extend([3, 4])
    >>> Q.extendleft([5, 6])
    >>> Q
    deque([6, 5, 2, 1, 3, 4])
    >>> Q.pop()
    4
    >>> Q.popleft()
    6
    >>> Q
    deque([5, 2, 1, 3])
    >>> Q.rotate(3)
    >>> Q
    deque([2, 1, 3, 5])
    >>> Q.rotate(-3)
    >>> Q
    deque([5, 2, 1, 3])
    
    >>> last_three = collections.deque(maxlen=3)
    >>> for i in range(4):
    ...     last_three.append(i)
    ...     print ', '.join(str(x) for x in last_three)
    ...
    0
    0, 1
    0, 1, 2
    1, 2, 3
    2, 3, 4
    

    Làm cách nào để loại bỏ một ký tự khỏi một chỉ mục trong một chuỗi trong Python?

    Str.replace () có thể có thể được sử dụng để thực hiện nhiệm vụ loại bỏ vì chúng ta có thể thay thế chỉ số cụ thể bằng char trống và do đó giải quyết vấn đề.str. replace() can possibly be used for performing the task of removal as we can replace the particular index with empty char, and hence solve the issue.

    Làm thế nào để bạn tìm thấy ký tự thứ n của một chuỗi trong Python?

    Để tìm sự xuất hiện thứ n của một ký tự trong chuỗi văn bản, bạn có thể sử dụng một công thức dựa trên các hàm tìm và thay thế.Trong ví dụ được hiển thị, công thức trong d5 là: = find (char (160), thay thế (b5, "@", char (160), c5)) trong ví dụ này, chúng tôi đang tìm kiếm sự xuất hiện thứ n của "@" "tính cách.use a formula based on the FIND and SUBSTITUTE functions. In the example shown, the formula in D5 is: =FIND(CHAR(160),SUBSTITUTE(B5,"@",CHAR(160),C5)) In this example we are looking for the nth occurrence of the "@" character.

    Làm thế nào để bạn thay thế tất cả các lần xuất hiện của một chuỗi trong Python?

    Phương thức thay thế () thay thế () là một phương thức tích hợp trong Python thay thế tất cả các lần xuất hiện của ký tự cũ bằng ký tự mới.replace() method replace() is a built-in method in Python that replaces all the occurrences of the old character with the new character.

    Làm thế nào để bạn xóa một chữ cái khỏi một chuỗi trong Python?

    Python loại bỏ ký tự khỏi chuỗi bằng cách sử dụng thay thế () nếu chúng ta cung cấp một chuỗi trống làm đối số thứ hai, thì ký tự sẽ bị xóa khỏi chuỗi.Lưu ý rằng chuỗi là bất biến trong Python, vì vậy hàm này sẽ trả về một chuỗi mới và chuỗi ban đầu sẽ không thay đổi.using replace() If we provide an empty string as the second argument, then the character will get removed from the string. Note that the string is immutable in Python, so this function will return a new string and the original string will remain unchanged.