Current post is on how to convert Left-Right Function of VBScript in Python 3.2.
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