Thursday, July 26, 2012

Python: Converting VbScript Left-Right Function

Current post is on how to convert Left-Right Function of VBScript in Python 3.2.


Converting or migrating a VbScript project to Python?

Stuck in converting Left or Right function of VbScript in Python?


VBScript Left Function:


Left(string, Length)

where,
          string = your text string
          length = number of character from left you want to extract

Example:

myleftstring = Left("Hello from Left, noting more!!!",15);

//myleftstring will contain 'Hello from Left'.


VBScript Right Function:


Right(string, Length)


where,
          string = your text string
          length = number of character from right you want to extract


Example:


myrightstring = Right("Hello from Left, noting more!!!",15);


//myrightstring will contain ', noting more!!!'.


Python: Array Slicing:


Array slicing is done using [ : ] operator.

Example:

text = 'Hello from Python 3.2'
leftslicedtext = text[:11]

//leftslicetext will contain 'Hello from'

rightslicedtext = text[11:]

//rightslicedtext will contain ' Python 3.2'

frombehind = text[-3:]

//frombehind will contian '3.2'

Summarize:


To convert the VbScript Left / Right Function, use the array slicing feature of Python.

It is simple, clean and readable.

Appreciate your feedback via comments. Thanks.

No comments:

Post a Comment

Disclaimer:

The above post and all the posts in the blog are derived from facts, information, logical interpretation and logical conclusion of printed and internet materials available to me, perceived and produced by 99 gm brain of mine, which by no means always be accurate, consistent and complete.

All the posts are for personal quick reference only.

If any suggestion, correction, misinterpretation, misconception commented, which will be moderated and deleted if required, to avoid unforeseen issues.

If any trademark / copywrite issue is present, do send in a mail and appropriate tag (logo, name, website link) will be attached to the same.

Additional disclaimer will be attached wherever required in the post.