python - Get characters of a string (from right) -


i'm looking best way file name of variable looks that:

 = 'this\is\a\path\to\file'  print a[-4:] 

i'm trying 'file' extracting last 4 letters print a[-4:] result is:

 ile 

if make print a[-5:] get:

 ofile 

i guess python has problem backslash, escaping did not help. how solve this? way or there more performant way "file" searching '\' right left?

\f single character (form-feed) in python. try doubling backslashes:

a = 'this\\is\\a\\path\\to\\file' 

or, equivalently:

a = r'this\is\a\path\to\file' 

after print a[-4:] print file.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -