Hướng dẫn how to subtract string in python - cách trừ chuỗi trong python

replace có thể làm điều gì đó mà bạn không muốn nếu chuỗi thứ hai có mặt tại một số vị trí:

s1 = 'AJYFAJYF'
s2 = 'AJ'
if s1.startswith[s2]:
    s3 = s1.replace[s2, '']
s3
# 'YFYF'

Bạn có thể thêm một đối số bổ sung vào replace để cho biết rằng bạn chỉ muốn một lần thay thế xảy ra:

if s1.startswith[s2]:
    s3 = s1.replace[s2, '', 1]
s3
# 'YFAJYF'

Hoặc bạn có thể sử dụng mô -đun re:

import re
if s1.startswith[s2]:
    s3 = re.sub['^' + s2, '', s1]
s3
# 'YFAJYF'

if s1.startswith[s2]:
    s3 = s1.replace[s2, '', 1]
s3
# 'YFAJYF'
0 là để đảm bảo rằng
if s1.startswith[s2]:
    s3 = s1.replace[s2, '', 1]
s3
# 'YFAJYF'
1 nó chỉ được thay thế ở vị trí đầu tiên của
if s1.startswith[s2]:
    s3 = s1.replace[s2, '', 1]
s3
# 'YFAJYF'
2.

Tuy nhiên, một cách tiếp cận khác, được đề xuất trong các bình luận, sẽ là loại bỏ các ký tự

if s1.startswith[s2]:
    s3 = s1.replace[s2, '', 1]
s3
# 'YFAJYF'
3 đầu tiên từ
if s1.startswith[s2]:
    s3 = s1.replace[s2, '', 1]
s3
# 'YFAJYF'
2:

if s1.startswith[s2]:
    s3 = s1[len[s2]:] 
s3
# 'YFAJYF'

Một số thử nghiệm sử dụng %thời gian ma thuật trong ipython [Python 2.7.12, ipython 5.1.0] cho thấy cách tiếp cận cuối cùng này nhanh hơn:

In [1]: s1 = 'AJYFAJYF'

In [2]: s2 = 'AJ'

In [3]: %timeit s3 = s1[len[s2]:]
The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 87.7 ns per loop

In [4]: %timeit s3 = s1[len[s2]:]
The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 87.8 ns per loop

In [5]: %timeit s3 = s1[len[s2]:]
The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 87.4 ns per loop

In [6]: %timeit s3 = s1.replace[s2, '', 1]
The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 230 ns per loop

In [7]: %timeit s3 = s1.replace[s2, '', 1]
The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 228 ns per loop

In [8]: %timeit s3 = s1.replace[s2, '', 1]
The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 234 ns per loop

In [9]: import re

In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 1.85 µs per loop

In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 1.86 µs per loop

In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 1.84 µs per loop

Xem thảo luận

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

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

    Lưu bài viết

    Đọc

    Phương pháp số 1: Sử dụng LOOP + Remove [] & NBSP; sự kết hợp của các chức năng trên có thể được sử dụng để thực hiện nhiệm vụ này. Trong đó, chúng tôi thực hiện loại bỏ các phần tử bằng cách sử dụng Remove [] và kiểm tra các phần tử tương tự bằng Loop. & NBSP; 
    The combination of above functionalities can be used to perform this task. In this, we perform the removal of elements using remove[] and check for similar elements using loop. 

    Python3

    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    5
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    6
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    7
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    8
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    9
    import re
    if s1.startswith[s2]:
        s3 = re.sub['^' + s2, '', s1]
    s3
    # 'YFAJYF'
    
    0
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    9
    import re
    if s1.startswith[s2]:
        s3 = re.sub['^' + s2, '', s1]
    s3
    # 'YFAJYF'
    
    222

    import re
    if s1.startswith[s2]:
        s3 = re.sub['^' + s2, '', s1]
    s3
    # 'YFAJYF'
    
    8
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    6
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    7
    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    1
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    9
    import re
    if s1.startswith[s2]:
        s3 = re.sub['^' + s2, '', s1]
    s3
    # 'YFAJYF'
    
    0
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    9
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    8
    import re
    if s1.startswith[s2]:
        s3 = re.sub['^' + s2, '', s1]
    s3
    # 'YFAJYF'
    
    7

    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    7
    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    8
    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    9
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    0
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    1
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    2

    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    7
    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    8
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    5
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    0
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    1
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    8

    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    9
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    6
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    1
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    2
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    3
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    4
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    5

    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    2
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    7
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    4
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    9

    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    0
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    1
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    7
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    4
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    4

    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    5
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    6

    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    7
    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    8
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    9
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    0
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    1replace2

    Đầu ra

    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']

    & nbsp; Phương thức #2: Sử dụng bộ đếm [] + phần tử [] & nbsp; Sự kết hợp của các hàm trên cung cấp tốc ký để giải quyết vấn đề này. Trong đó, chúng tôi trích xuất số lượng các phần tử trong cả hai danh sách và sau đó thực hiện tách bằng cách trích xuất bằng phần tử [].
    Method #2 : Using Counter[] + elements[] 
    The combination of the above functions provides a shorthand to solve this problem. In this, we extract the count of elements in both list and then perform separation by their extraction using element[].

    Python3

    replace3 replace4replace5 replace6

    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    5
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    6
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    7
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    8
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    9
    import re
    if s1.startswith[s2]:
        s3 = re.sub['^' + s2, '', s1]
    s3
    # 'YFAJYF'
    
    0
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    9
    import re
    if s1.startswith[s2]:
        s3 = re.sub['^' + s2, '', s1]
    s3
    # 'YFAJYF'
    
    222

    import re
    if s1.startswith[s2]:
        s3 = re.sub['^' + s2, '', s1]
    s3
    # 'YFAJYF'
    
    8
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    6
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    7
    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    1
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    9
    import re
    if s1.startswith[s2]:
        s3 = re.sub['^' + s2, '', s1]
    s3
    # 'YFAJYF'
    
    0
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    9
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    8
    import re
    if s1.startswith[s2]:
        s3 = re.sub['^' + s2, '', s1]
    s3
    # 'YFAJYF'
    
    7

    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    7
    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    8
    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    9
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    0
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    1
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    2

    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    7
    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    8
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    5
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    0
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    1
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    8

    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    9
    if s1.startswith[s2]:
        s3 = s1.replace[s2, '', 1]
    s3
    # 'YFAJYF'
    
    6
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    1
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    2
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    3
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    4
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    5

    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    7
    if s1.startswith[s2]:
        s3 = s1[len[s2]:] 
    s3
    # 'YFAJYF'
    
    8
    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']
    9
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    0
    In [1]: s1 = 'AJYFAJYF'
    
    In [2]: s2 = 'AJ'
    
    In [3]: %timeit s3 = s1[len[s2]:]
    The slowest run took 24.47 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.7 ns per loop
    
    In [4]: %timeit s3 = s1[len[s2]:]
    The slowest run took 32.58 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.8 ns per loop
    
    In [5]: %timeit s3 = s1[len[s2]:]
    The slowest run took 21.81 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000000 loops, best of 3: 87.4 ns per loop
    
    In [6]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 230 ns per loop
    
    In [7]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 17.79 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 228 ns per loop
    
    In [8]: %timeit s3 = s1.replace[s2, '', 1]
    The slowest run took 16.27 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 234 ns per loop
    
    In [9]: import re
    
    In [10]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 82.02 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    
    In [11]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 12.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.86 µs per loop
    
    In [12]: %timeit s3 = re.sub['^' + s2, '', s1]
    The slowest run took 13.08 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.84 µs per loop
    
    1replace2

    Đầu ra

    The original list 1 : ['gfg', 'is', 'best', 'for', 'CS']
    The original list 2 : ['preferred', 'is', 'gfg']
    The Subtracted list is : ['best', 'for', 'CS']


    Tôi có thể trừ hai chuỗi trong Python không?

    Bạn có thể trừ 2 chuỗi trong Python không?Chuỗi Python không có toán tử trừ.The Python string doesn't have a subtraction operator.

    Làm thế nào để bạn trừ các phần tử trong một chuỗi python?

    Phương pháp số 1: Sử dụng Loop + Remove [] Trong này, chúng tôi thực hiện việc loại bỏ các phần tử bằng cách sử dụng Remove [] và kiểm tra các phần tử tương tự bằng Vòng lặp.Using loop + remove[] In this, we perform the removal of elements using remove[] and check for similar elements using loop.

    Chúng ta có thể trừ 2 chuỗi không?

    Chuỗi gần như vô hạn, do đó bạn có thể sử dụng chúng để lưu trữ "số" lớn.Để trừ 2 số, bạn trừ từng chữ số riêng lẻ và mang theo các mượn.Đây là trường hợp tương tự trong lập trình.To subtract 2 numbers, you subtract each digit individually and carry over the borrows. This is the same case in programming.

    Làm thế nào để bạn trừ các chữ cái trong Python?

    Trừ các chữ cái trong Python..
    a = "a" x = ord [a] # -> Điều này sẽ cho tôi 97 ..
    a = 97 b = 98 ... z = 122 ..
    B - 14 = A - 13 = Z - 12 [...], v.v.

    Bài Viết Liên Quan

    Chủ Đề