0

I am trying to write a function strip white-spaces from the left hand side a string.

def main():
    str_strip = input('enter a string of which whitespace are stripped: ')
    cs-strip(str_strip)

def cs-strip(str_strip):
    i = 0
    temp = list(str_strip)
    if  temp[0] == ' ':
        temp.remove(temp[0])
    elif temp[0] == '\' :
        if temp[1] == 'n' or temp[1] == 't':
            temp.remove(temp[0])
            temp.remove(temp[0])
    new = ''.join(map(str,temp))
    print(new)

main()

But I cant quote compare '\' with other strings, I have tried '''\''', it still doesn't work. How can I compare string when string is or includes '\'?

Byron
  • 43
  • 5

2 Answers2

0

Use '\\' or r'\' to compare with backslash like below.

temp[0] == '\\'
temp[0] == r'\'
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
0

All you need is to escape it with other \ char, so your elif should be like this:

elif temp[0] == '\\' :
Or Duan
  • 13,142
  • 6
  • 60
  • 65