Search code examples
pythonstringcharacter

delete characters in a string from begining to ']' character


Delete characters upto and including ']'

a="[12] hi how are you [1]" 
b="[13][14] hello" 

expected output :

a="hi how are you [1]"

b=" hello"  

Solution

  • You can use regular expression to achieve this

    import re
    
    txt = "[12] hi how are you" 
    x = re.sub("\[[0-9]+\]\s*", "", txt)
    print(x)