Use Python


1.If you are not sure what type a value has, the interpreter can tell you.

>>> type("Hello, World!")
<type 'string'>
>>> type(17)
<type 'int'>

Not surprisingly, strings belong to the type string and integers belong to the type int. Less obviously, numbers with a decimal point belong to a type called float


2.Interestingly, the + operator does work with strings, although it does not do exactly what you might expect. For strings, the + operator represents concatenation, which means joining the two operands by linking them end-to-end. For example:

fruit = "banana"
bakedGood = " nut bread"
print fruit + bakedGood

The output of this program is banana nut bread. The space before the word nut is part of the string, and is necessary to produce the space between the concatenated strings.

The * operator also works on strings; it performs repetition. For example, 'Fun'*3 is 'FunFunFun'. One of the operands has to be a string; the other has to be an integer.

posted @ 2006-03-21 17:26  shipfi  阅读(236)  评论(0编辑  收藏  举报