-3

I would need to stripoff "domain\" from "domain\name" to extract name which can be any name or the word name literally

>>> s="domain\name"

>>> x=s[5:]
>>> print(x)
n
ame
>>> s="domain\bh16"
>>> x=s[5:]
>>> print(x)
h16
>>> x=s[4:]
>>> print(x)
ih16
ananda
  • 21
  • 2
  • 8

1 Answers1

2

You can convert it to a raw string and use replace as normal

s = r"domain\bh16"

print(s.replace("domain\\", '')) #bh16
Tonechas
  • 13,398
  • 16
  • 46
  • 80
datawrestler
  • 1,527
  • 15
  • 17
  • the solution you gave me works fine when I know the value of variable 's'. However, I am using it in a code where s gets assigned dynamically to different names with 'domain\' prefixed to it . If I try that way the solution is not working >>> Username='corp\bh16' >>> y=r"Username" >>> print(y) Username – ananda Dec 15 '16 at 00:09