close

How to subtract strings in python

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to subtract strings in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to subtract strings in python?

  1. How to subtract strings in python?

    I think what you want is this:
    a = 'AJYF' b = a.replace('AJ', '') print b # produces 'YF' a = 'GTYF' b = a.replace('GTY', '') print b # produces 'F'

  2. subtract strings in python

    I think what you want is this:
    a = 'AJYF' b = a.replace('AJ', '') print b # produces 'YF' a = 'GTYF' b = a.replace('GTY', '') print b # produces 'F'

Method 1

Easy Solution is:

>>> string1 = 'AJYF'
>>> string2 = 'AJ'
>>> if string2 in string1:
...     string1.replace(string2,'')
'YF'
>>>

Method 2

I think what you want is this:

a = 'AJYF'
b = a.replace('AJ', '')
print b     # produces 'YF'
a = 'GTYF'
b = a.replace('GTY', '')
print b     # produces 'F'

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read