Python for Visual Basic or C# programmers

A

A.M

Hi,



I am trying to find the equivalent functions such as vb's str or asc in
Python. Is there any resource that help me to find these kinds of functions
in Python faster?



Thank you,

Alan
 
M

Metalone

A.M said:
Hi,



I am trying to find the equivalent functions such as vb's str or asc in
Python. Is there any resource that help me to find these kinds of functions
in Python faster?



Thank you,

Alan

Python has a str() function that is close to vb's str. The Python
version does not produce a leading space.
str(123) --> '123'

If you want to control the formatting to mimick vb or create different
formatting you can use.
" %d" % 123 --> ' 123'
The format specifier is just like 'C's printf format strings.
A tuple should follow the % sign if formatting more than 1 number.
"%d %d" % (123, 456)

Python has a similar function to vb's asc.
It is ord().
ord() accepts a single character, whereas asc operates on the first
character of a string.
To mimick vb you could do
s = 'hello'
ord(s[0]) --> 104

I am unaware of any reference associating vb functionality with Python
functionality.
Then again, I never looked for one.
 
M

Metalone

Slight correction.
" %d" % 123 is not quite equivalent to str(123) as it does not handle
negative numbers the same way.
I am not sure there is a simple direct equivalent.
"%+d" % 123 --> "+123" always gives a sign.
"%4d" % 123 --> " 123"
"%4d" % -123 --> "-123" so this works if you you know how wide the
number is.

This might be a little too tricky.
[" %d", "%d][n < 0] % n --> selects list[0] or list[1] based upon sign
of number
 
M

Max

Metalone wrote:
This might be a little too tricky.
[" %d", "%d][n < 0] % n --> selects list[0] or list[1] based upon sign
of number

("%+d" % 123).replace("+", " ") is slightly longer but instantly
comprehensible, although I for one think your boolean indexing trick is
cool.

--Max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,282
Latest member
Xyprime

Latest Threads

Top